3 Basic JavaScript Concepts That Are Important.
Manav
Posted on January 7, 2021
Learning javascript is weird. I want to share 3 things that I learnt today in javascript.
Hoisting
Hoisting is a mechanism where variable or function declarations are moved to the top of their scope before they're executed. That means, if we do this:
console.log(greeter);
var greeter = "say Hello!";
It'll be interpreted as this:
var greeter;
console.log(greeter); //undefined
greeter = "say Hello!";
null vs undefined
null
is a value, just like 2, 3.5 or true. Setting a variable to null
means that you have declared a variable and decided to give the value - nothing, to it.
undefined
on the other hand, means you have declared a variable but not given it any value yet.
Interestingly, null
loosely equalizes to undefined
but there are a few gotchas!
console.log(null == undefined); //true
console.log(null === undefined); //false
console.log(null >= 0); //true
console.log(null <= 0); //true
console.log(undefined <= 0); //false
console.log(undefined >= 0); //false
==
vs ===
This is a simple one. ==
means loose equality. It's used when you want to know if two values are equal if they'd have same type. For example:
console.log(0 == false); //true
console.log(1 == 1.0); //true
console.log(1 == '1'); //true
===
however, checks for strict equality. It means, it'll only return true IF the two values are absolutely same. For example:
console.log(0 === false); //false
console.log(1 === 1.0); //true, it's a bad idea to compare floats anyway.
console.log(1 === '1'); //false
That's all folks!
Posted on January 7, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.