DEV Community

Discussion on: Start Javascript: learn about variables

Collapse
 
curtisfenner profile image
Curtis Fenner

I'm going to be nitpicky.

Variables in JavaScript do not have types. They have values, and those values have types. This is important to understand JavaScript, as it is to understand other dynamically typed languages; there is no restriction on which types of values any given variable can take on.

The JavaScript engine is capable of guessing (or "infer") the type of a variable based on its value

JavaScript does not do any inference, as it has no static type system. This is called reflection, and it is not a guess -- the language is mandated to keep the bookkeeping records which make it possible to determine, dynamically, what the type of any value is (again, not variable, but value).

Collapse
 
mindsers profile image
Nathanaël CHERRIER • Edited

Thanks for reading and sharing your thoughts. I really appreciate.

Talking about if it's the variable or its value that has a type is like asking if it's the egg or the chicken that appears first. Given that in JavaScript a variable always has a value and a value always has a type, I think we can say a variable has a type even if this type can change (when the value change though).