DEV Community

Cover image for 3 Basic JavaScript Concepts That Are Important.
Manav
Manav

Posted on

3 Basic JavaScript Concepts That Are Important.

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!";
Enter fullscreen mode Exit fullscreen mode

It'll be interpreted as this:

var greeter;
console.log(greeter); //undefined
greeter = "say Hello!";
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

== 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
Enter fullscreen mode Exit fullscreen mode

=== 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
Enter fullscreen mode Exit fullscreen mode

That's all folks!

Top comments (2)

Collapse
 
anirudh711 profile image
Anirudh Madhavan

Hoisting can be done only on variables defined using 'var' and function definitions excluding function expressions. In the above two cases , variables are partially hoisted (undefined) and functions are fully hoisted.

Collapse
 
sigmapie8 profile image
Manav

Ah. Thanks for that new information!