I'm trying to let i
be the square root of -1 (sqrt(-1)
). It is a defined value:
i = sqrt(-1)
var i = sqrt(-1);
fill(0, 0, 0);
text(i, 25, 25);
It gives me an error. What happened?
sqrt()
is a function that already has a defined behavior. From MDN:
The square root of the given number. If the number is negative,
NaN
is returned.
NaN
is not an error in itself. It's a value that means "not a number" - you can find more information here.
You seem to be thinking about functions and values in a different direction than I would. Looking at this line:
var i = sqrt(-1);
This line isn't saying "define the square root of -1 to be equal to i". It's saying: create a variable named i
, and set its value equal to whatever is returned by calling sqrt(-1)
.
So the code calls sqrt(-1)
, which returns NaN
, so the value of i
is NaN
. Then when you try to use that value, you'll see NaN
.