Home Using letter Pi(π) in JavaScript

Using letter Pi(π) in JavaScript

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:

Uncaught ReferenceError: π is not defined

What? Is Pi sign a valid variable in JavaScript?


The official ECMA Script documentation is quite complicated. But we can simplify this to (by Mathias Bynen):

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

var π = Math.PI;

Isn’t this cool?

This post is licensed under CC BY 4.0 by the author.