58
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.;var a = 2
;console.log(a)
The above code snippet will display 2 in the console without throwing any error!
var b = 5 + '9';
console.log(b);
The above code snippet will display "59" in the console!
NaN == NaN // -> false
NaN === NaN // -> false
[] == true // -> false
[] === true // -> false
[] == false // -> true
[] === false // -> false
{} == {} // -> false
{} === {} // -> false
{} >= {} // -> true
{} > {} // -> false
Things got a bit messed up, right?
(function() {
console.log('works well');
})();
function() {
console.log('generates syntax error');
}();
Here the first function works fine as it is a IIFE but the second one generates
SyntaxError
.function f1() {
return
{
grade: 'A+'
}
}
function f2() {
return {
grade: 'A+'
}
}
typeof f1() === typeof f2(); // -> false
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
58