The tricky parts or ‘gotchas’ (not limited to the following) are:
- == vs ===
- Case sensitivity
- Automatic Semi-colon Insertion
- ‘ == ‘ is not the same as ‘===’ : Not all equality signs mean the same. The double equal operator follows type coercion which means that both the data types of the variable do not need to match.The triple equality operator is a strict equality operator i.e it will perform a type coercion.
console.log( 12 ===
If you run this on the console, it gives the following output:
This means that when we use the ‘==’ operator it converts the string of 12 into a number and compares the numbers. Since they are the same numbers it returns a true value. In ‘===’ operator it follows strict type and checks the data type and value. As one data type is of string and the other is a number type, it returns a false value.
the first character of an identifier could be a letter or a dollar sign but it cannot be a number. Two variables need not be the same. Example:
age ="20"; Age = "23";
Here, Age and age are two separate string variables.
The parseInt function parses a string into a number.it takes two arguments:
- The first argument- Takes the string you want to parse.
- The second argument- The base of the number.The range can be from 2 to 36.
In most cases, the default base is a radix 10. However if the number begins with 0x then assume that the string will be parsed to base 16 and if the number begins with 0 assume the base to be 8.
In the above code at first, it would seem that they return the same object but the second object fails to return and returns a value undefined due to ASI.