Open In App

What are the Gotchas in JavaScript ?

Last Updated : 08 May, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Javascript truly is a popular language due to its simplicity and versatility. Despite having many merits, Javascript is a funny language that can confuse you at times especially for those accustomed to the traditional OOP language.

    The tricky parts or ‘gotchas’ (not limited to the following) are:

  1. == vs ===
  2. Case sensitivity
  3. parseInt
  4. 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.
    Example:




    console.log(12 == '12')
    console.log( 12 === '12')

    
    

    If you run this on the console, it gives the following output:

    true
    false

    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.

  • Case sensitive : Identifiers are used to name keywords, variables and functions.Like most programming languages, the rules for naming a variable are also the same. As javascript is case-sensitive,
    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.

    Note: Javascript follows camel case notation.

  • parseInt:
      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.

    Syntax:

    parseInt(string, radix)

    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.
    Example:




    parseInt("02", 10); 
    parseInt("0x11"); 

    
    

    Output:

     2
     17
  • Automatic semi-colon insertion: Although semi-colons are optional in Javascript it is a good practice to add them wherever needed. This is because javascript automatically adds semi-colon to your code and could throw an error. Consider a classic example of ASI(Automatic semi-colon insertion):




    function foo1()
    {  
    return {       
    bar: "hello"  
    };
      
    function foo2()            
    {  
    return   {     
     bar: "hello" 
     };
     }
      
    console.log("foo1 returns:");
    console.log(foo1());
    console.log("foo2 returns:");
    console.log(foo2());

    
    

    Output:




    foo1 returns:
     Object {bar: "hello" }
     foo2 returns:
     undefined

    
    

    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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads