Some Important JavaScript concepts you must know!!
Published: 2021-12-21 17:56:02

**Differences between “==” and “===”**

Double equal (==) checks only the value equality and also converts the value into the same type, whereas triple-equal (===)  strictly checks value equality and also it does not convert the value into the same type. If a variable's value is same but their type is different then it will return false.

_Example:_

'1' == 1    =>  true

1 === '1'   =>  false

**Truthy and Falsy values**

**Truthy values**

true
Any number (positive or negative)
Any string including white space , ‘o’ ,’false’
[ ] ,{ }
Anything else that is not falsy will be truthy


**Falsy values**

false
0
Empty string
undefined,null
NaN

**Recursion**

Recursive is a function that can call itself.It can solve any problem just by calling itself. As it calls itself , it can become infinity, so to stop it from becoming infinity we need to set a terminator point . When it hits a terminator point, it will be terminated and the recursive function will stop there, which means it will return .

_Example:_

function countDownFrom(number) {
	if (number === 0) {
		return;
	}
    console.log(number);    
    countDownFrom(number - 1);
}
countDownFrom(5);
// 5
// 4
// 3
// 2
// 1

**Scope**

In Javascript,the availability or usability of a variable or other resource in our code is defined by its scope.

**Block Scope**

When a variable is declared inside { } curly brackets or block  and cannot  be accessed  from this block then it is known as Block scope.

_Example_

var x = 1;
let y = 1;

if (true) {
  var x = 2;
  let y = 2;}
console.log(x);
// expected output: 2

console.log(y);
// expected output: 1

**Global Variable**

A variable which has been declared outside the function and can be accessed from anywhere  globally then it is known as Global Variable.


**Global Scope**

Example:

A Scope which refers to all the variables declared in a JS file that aren't even inside any function and also it’s variables can be accessed from anywhere in the file.This type of scope is known as Global scope.

myVar = 1; // An automatic global variable

// An automatic global function
function myFunc() {
    return true;
}

**Window**

This is very useful for browsers when a user wants to go forward,backward,an existing page and another page, then it can easily go with the help of a window object.

Example:

window.open() - open a new window
window.close() - close the current window
window.moveTo() - move the current window

**Closure**

In closure,an outside function can run the inner function and the inner function can still access variables used by the outer one.
It will be very useful when we will create private variables and functions to hide implementation details in javascript.

_Example_


function init() {
  var name = 'Mozilla'; // name is a local variable created by init
  function displayName() { // displayName() is the inner function, a closure
    alert(name); // use variable declared in the parent function
  }
  displayName();
}
init();

**Encapsulation**

A mechanism which restricts direct access to some of the object’s component.At the same it also bundles data with methods that can operate on that data.

_Example:_

var employee = {
name : "Aditya Chaturvedi",
};
alert(employee.name); // Aditya Chaturvedi
employee.name = "Rahul Khanna";
alert(employee.name); // Rahul Khanna


**Bind()**

Bind() method returns a new function.It also helps us to bind multiple objects to a new function.it’s ‘this’ keyword which allows us to add new parameters in bind().

_Example:_

const module = {
  x: 42,
  getX: function() {
    return this.x;
  }
};

const unboundGetX = module.getX;
console.log(unboundGetX()); // The function gets invoked at the global scope
// expected output: undefined

const boundGetX = unboundGetX.bind(module);
console.log(boundGetX());
// expected output: 42

**call()**

This method calls  a function whose first parameter will consider the object as ‘this’ value .
It will also take parameters separately. Comma used for separation.

_Example:_

function Product(name, price) {
  this.name = name;
  this.price = price;
}

function Food(name, price) {
  Product.call(this, name, price);
  this.category = 'food';
}

console.log(new Food('cheese', 5).name);
// expected output: "cheese"


**Apply()**

This method calls  a function whose first parameter will consider the object as ‘this’ value.It takes parameters as an array.

_Example
_
const person = {
    firstName: 'John',
    lastName: 'Doe'
}

function greet(greeting, message) {
    return `${greeting} ${this.firstName}. ${message}`;
}

let result = greet.apply(person, ['Hello', 'How are you?']);

console.log(result);

// expected output: Hello John. How are you?