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).
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).
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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.
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).
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).