Important Topics Of JavaScript

Rezowan Ahmed
4 min readNov 5, 2020
JavaScript

Truthy and Falsy values in JavaScript

Truthy

In JavaScript, a truthy value is a value that is considered true when encountered in a Boolean context.

if (true)
if ({})
if ([])
if (16)
if ("0")
if ("false")
if (new Date())
if (-16)
if (4n)
if (11.14)
if (-11.14)
if (Infinity)
if (-Infinity)

Example:

const number = 16;if (number) {console.log("Condition is true");}else {console.log("Condition is false");}// Condition is true

Falsy

A falsy value is a value that is considered false when encountered in a Boolean context.

if (false)
if (null)
if (undefined)
if (0)
if (-0)
if (0n)
if (NaN)
if ("")

Example:

const number = 0 ;if (number) {console.log("Condition is true");}else {console.log("Condition is false");}// Condition is false

Null & Undefined

Null and Undefined are both data types in JavaScript. Undefined is a variable that has been declared but not assigned a value. So you can assign the value null to any variable which basically means it’s blank. So by not declaring a value to a variable, JavaScript automatically assigns the value to undefined. null == undefined; // true

Example of Null

var name = null;
if (name === null) {
console.log("name is null");
}
else {
console.log("name is not null");
}
// name is null

Example of Undefined

var name;
if (name === undefined) {
console.log("name is undefined");
}
else {
console.log("name is not undefined");
}
// name is undefined

Double Equal (==)

The double equality operator (==) checks whether its two operands are equal, returning a Boolean result. Double equality will not check two operands that are the same type or different type. It will check just value. Double equality has its own algorithm to check value.

const num1 = 2;const num2 = "2";
if (num1 == num2) {
console.log("Condition is true");
}
else {
console.log("Condition is false");
}
// Condition is true

Triple equal (===)

This triple equal is called in JavaScript is Strict equality. The Strict equality operator (===) checks whether its two operands are equal, returning a Boolean result. Strict equality will strictly check two operands that are the same type or not. It will check the value with type both.

const num1 = 2;  //Integer typeconst num2 = "2";  // String type
if (num1 == num2) {
console.log("Condition is true");
}
else {
console.log("Condition is false");
}
// Condition is false

Scope

Scope in JavaScript refers to the current context of code, which determines the accessibility of variables to JavaScript. In JavaScript, there are two types of scope. The two types of scope are local and global:

Local scope

Local variables are those declared inside of a block. Local variables can only be accessed from within the block.

function add (num1, num2) {
let sum = num1 + num2;
return sum;
}
const result = add(5, 3);
//console.log(sum); // I can't access from outside of block scope so I will get error.
console.log(result)

Global scope

Global variables are those declared outside of a block. A global variable has a global scope. All scripts and functions on a web page can access it.

let number = 16;
function add (num1, num2) {
let sum = num1 + num2 + number;
console.log(number); // I can access inside block scope
return sum;
}
const result = add(5, 3);
console.log(result) //24

JavaScript Hoisting

Variable hoisting means the JavaScript engine moves the variable declarations to the top of the script. Again, Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution.

variable hoisting
console.log(name); // undefined 
var name = hoisting;

However, the first line of code doesn’t cause an error because the JavaScript engine moves the variable declaration to the top of the script. Technically, the code looks like the following in the execution phase:

var name;console.log(name); // undefined
name = hoisting;

If you write like this the execution will be correct.

var name = hoisting;
console.log(name) //hoisting
Scope & Hoisting

Thank you for reading this article…

--

--