Are JavaScript Variables All the Same?

One thing that amazed me when I first started to learn JavaScript, is not being able to specify the type of variable being declared. For example in C++, we specify the type before the variable name, like this: int myNumber, float myFloat, char myCharacter.

The term “loosely typed language” refers to any language for which the type of a variable isn’t specified by the programmer – JavaScript is one such language.

However, JavaScript does indeed have primitive types, for example: number, boolean, and string. It’s just that any given variable’s value is automatically converted to the appropriate type by JavaScript, depending on the circumstance in which the variable is being used. For example, when adding a number to a string, the number is converted to a string before concatenation occurs.

Another thing that really amazed me to begin with, was being able to store functions inside variables, and then do things such as pass the variable to another function, or store it inside an array, and then invoke that variable as if it was the function itself.