/ Software Development

6 Interesting JavaScript Facts

Md. Mahmud Ur Rahman

Md. Mahmud Ur Rahman

Read more posts by this author.

Read More
6 Interesting JavaScript Facts

JavaScript or JS is becoming more and more popular each day. It is a flexible and powerful programming language that is implemented consistently by various web browsers. JavaScript can be used in both the front-end and back-end of web development.

JavaScript offers a wide range of frameworks and libraries that help developers create complex applications with low overhead. Programmers can import libraries and frameworks in their code to augment their application’s functionality.

Knowledge on JS is also important when you are organizing a website with better UI/UX.

But there are some fun facts about JavaScript. In this article we’ll talk about some strange things in this language’s syntax and semantics.

JavaScript Fun Facts:

1. Immediately-Invoked Function Expression

Look at the examples below. First one works but second one gives Uncaught SyntaxError: Unexpected token ).

// Works fine
var foo = function () { 
    //some stuff
}();

// Syntax error
function foo() {
    //some stuff 
}();

2. Start with a Semicolon!

In many other programming languages you have to put semicolon(;) in the end of a statement. In JavaScript you can start with a semicolon(;).

;var a = 2
javascript interesting facts

3. Parentheses Position Matters

function foo() {
   return
   {
      foo: 'bar'
   }
}

function bar() {
   return {
      foo: 'bar'
   }
}

typeof foo() === typeof bar(); //false

4. Equal is Not Always Equal

Let’s look at this situation:

1 == 1; //true
'foo' == 'foo'; //true
[1,2,3] == [1,2,3]; //false

this is absolutely usual thing – one is equal to one and foo is foo. The array [1,2,3] is not equal to [1,2,3] because it’s simply a reference type. That’s common for most languages. But what if we:

new Array(3) == ",,"; //true
javascript fun facts

5. Comparing objects

Let’s create two objects and keep their references in two variables:

var o1 = {},
    o2 = {};

We have two different references so:

o1 == o2 //false
o1 === o2 //false

Nothing weird at all.

Lets try this in JavaScript:

o1 >= o2 //true
o1 <= o2 //true
o1 != o2 //true
o1 !== o2 //true

Mmm, strange, isn’t it?

6. Sum of Objects and Arrays

1 * 'foo'; //NaN
1 * 2; //2
'foo' * 1; //NaN
'foo' + 1; //'foo1'
[] + []; //""
[] + {}; //[object Object]

Pretty strange, right?

+!+[] // output is 1

Conclusion

These are few of the JavaScript fun facts. Few of these strange things of JavaScript’s syntax and semantics can be dangerous in our daily work. So we should be careful on using them. Others are funny and good to know but all of them are awesome! 😁

Here you can also check other JavaScript articles like Nuxt.js for Vue Application.

Did you faced any of these or other JavaScript interesting facts? Let us know through the comment bellow.