DEV Community

Cover image for Some interesting facts about javascript
Mohammad Abdul Alim
Mohammad Abdul Alim

Posted on • Edited on

2 1

Some interesting facts about javascript

We all know that javascript is one of the most popular programming languages now-a-days. Javascript is a very strange language in deed. One reason is it has got syntax similar to C, C++ and Java but semantically this is not similar which makes developers confused. Another weird thing to mention is its prototype inheritance which can be achieved similarly using es6 class. Let us discuss some interesting facts about this very language.

  • Many programming languages use semi colon at the end of a statement. Javascript does so but you can also use semi colon at the beginning of a statement.
;var a = 2
;console.log(a)
Enter fullscreen mode Exit fullscreen mode

The above code snippet will display 2 in the console without throwing any error!

  • In javascript you can add a number with a string. The result will be a string without any error.
var b = 5 + '9';
console.log(b);
Enter fullscreen mode Exit fullscreen mode

The above code snippet will display "59" in the console!

  • In javascript the comparison operators act really weird in many cases. Lets see some examples:
NaN == NaN // -> false
NaN === NaN // -> false
[] == true // -> false
[] === true // -> false
[] == false // -> true
[] === false // -> false
{} == {} // -> false
{} === {} // -> false
{} >= {} // -> true
{} > {} // -> false
Enter fullscreen mode Exit fullscreen mode

Things got a bit messed up, right?

  • Javascript has a nice feature named Immediately Invoked Function Expression where a function can be executed just after it has been defined without being called explicitly.
(function() {
  console.log('works well');
})();

function() {
  console.log('generates syntax error');
}();
Enter fullscreen mode Exit fullscreen mode

Here the first function works fine as it is a IIFE but the second one generates SyntaxError.

  • In javascript the difference in parenthesis position can make two functions different.
function f1() {
   return
   {
      grade: 'A+'
   }
}
function f2() {
   return {
      grade: 'A+'
   }
}
typeof f1() === typeof f2(); // -> false
Enter fullscreen mode Exit fullscreen mode
  • In javascript undefined is not a reserved word although it a has a special meaning. This is the only way to determine whether a variable is undefined but the following code snippet looks quite weird.
undefined = "I am defined now!";
var c;
console.log(c == undefined); // -> false
Enter fullscreen mode Exit fullscreen mode

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay