Today I was doing some math in JavaScript. I needed a simple calculation from degrees to radians. Of course, I didn’t remember the formula. I copied it from Rapid Tables but I forgot to replace π char. Unexpectedly I received a normal error:
1
Uncaught ReferenceError: π is not defined
What? Is Pi sign a valid variable in JavaScript?
Documentation
The official ECMA Script documentation is quite complicated. But we can simplify this to (by Mathias Bynen):
1
An identifier must start with $, _, or any character in the Unicode categories “Uppercase letter (Lu)”, “Lowercase letter (Ll)”, “Titlecase letter (Lt)”, “Modifier letter (Lm)”, “Other letter (Lo)”, or “Letter number (Nl)”.
This allows us to write following code from ES5
1
var π = Math.PI;
Isn’t this cool?