Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptMost 5 Weird Behavior of JavaScript

Most 5 Weird Behavior of JavaScript

In this article, we will be talking about the weird facts of JavaScript. JavaScript is famous for its wide acceptance and also famous for its weirdness. The examples that are given below can blow the mind of the developers who came from C++, Java, C#, and other languages. JavaScript is an object-oriented programming language but the class concept was introduced in ES6. You can write HTML comments in JavaScript, min is greater than max and many more oddities of JavaScript will be covered here.

1. HTML comments in JavaScript: You can write HTML comments in JavaScript for their validity. You can use HTML tag <!– –>  in your JavaScript code without any error because these comments are treated as // of the JavaScript comment. These comments are used in the <script> tag for the old browser that does not understand JavaScript.

  • Example:

    Javascript




    <script>
      console.log("Hello world");
      <!-- console.log('Hello World') -->
    </script>

    
    

    Output :

    Hello world

 

2. Min is greater than Max: The Math.min() is greater than Math.max()

  • Example 1:

    Javascript




    <script>
      var a = Math.max() < Math.min();
      var b = Math.max() > Math.min();
      
      console.log(a);
      console.log(b);
    </script>

    
    

    Output :

    true
    false
  • Example 2: One more thing that is Math.max() returns -Infinity and Math.min() returns Infinity

    Javascript




    <script>
      var a = Math.max();
      var b = Math.min();
      
      console.log(a);
      console.log(b);
    </script>

    
    

    Output :

    -Infinity
    Infinity

3. In JavaScript true + true + true == 3 You can see true + true + true is equal to 3.

  • Example 1: This returns 3 because when we add two booleans (true or false) then they automatically typecast into number. In this example, the true is typecasted into number and when we type cast true to a number then we get 1 and when we type cast false into number then we get 0.

    Javascript




    <script>
      var a = true + true + true;
      
      console.log(a);
    </script>

    
    

    Output: So true + true + true becomes 1 + 1 + 1, hence we got the result 3.

  •  

  • Example 2:

    Javascript




    <script>
      let a = Number(true);
      let b = Number(false);
      
      console.log(a);
      console.log(b);
    </script>

    
    

    Output:

    1
    0
  • Example 3: Here are some interesting result related to this example.

    Javascript




    <script>
      var a = true + true;
      var b = true + false;
      var c = false + false;
      var d = 2 * true;
      var e = 2 * false;
      
      var f = (true + true) * (true + true) - true;
      console.log(a, b, c, d, e, f);
    </script>

    
    

    Output:

    2
    1
    0
    2
    0
    3

4. NaN is not NaN: NaN is not equal to NaN even when we use strict equality operator (===).

  • Example:

    Javascript




    <script>
      var a = NaN;
      var b = NaN;
      
      console.log(a == b);
      console.log(a === b);
    </script>

    
    

    Output:

    false
    false

5. The true is false: When we compare the string true and string false after applying two ! operator, then we get true.

  • Example 1: When we apply two logical not operator, we get true.

    Javascript




    <script>
      console.log(!!"true" == !! "false");
      console.log(!!"true" === !! "false");
      console.log(!!"Hello" == !! "world");
    </script>

    
    

    Output:

    true
    true
    true
  • Example 2: When we compare true and true, we get true.

    Javascript




    <script>
      console.log(!!"Geeksforneveropen");
      
      console.log(!!"true");
    </script>

    
    

    Output:

    true
    
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments