3 Basic JavaScript Concepts That Are Important.

sigmapie8

Manav

Posted on January 7, 2021

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!

💖 💪 🙅 🚩
sigmapie8
Manav

Posted on January 7, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Day 02: 30 Days of Codewars.js
challenge Day 02: 30 Days of Codewars.js

April 23, 2020

Day 01: 30 Days of Codewars.js
challenge Day 01: 30 Days of Codewars.js

April 22, 2020