10 critical things in JavaScript

Belal Hossain
3 min readMay 10, 2021

--

1. What is truthy or falsy values in Javascript?

Ans: In JavaScript, a truthy value is a value that translates to true when evaluated in a Boolean context. On the other hand, a falsy value is a value that translates to false when evaluated in a Boolean context. Flasy values are:

  1. 0
  2. False
  3. “ ”
  4. undefined
  5. null
  6. NaN

All values are truthy unless they are defined as falsy.

2. What is the difference between null and undefined in JavaScript?

Undefined means a variable has declared, but it has no value assigned. For example:

Const person;

Console.log(person);

// Undefined.

A function that doesn’t have a return statement implicitly returns undefined. For example:

const add = (a, b) => {

const result = a + b;

return;

}

let result = add(10,20);

console.log(result);

// undefined

According to MDN, null represents the intentional absence of any object value. Null must be assigned. For example:

Const person= null;

Console.log(person);

// null.

3. What is the difference between double equal (==) and triple equal (===) in JavaScript?

Ans: Double equals (==) is abstract equality comparison operator, which transforms the operands to the same type before making the comparison.

For example:

4 == 4 // true

‘4’ == 4 //true

Triple equals (===) are strict equality comparison operator, which returns false for different types and different content.

For example:

4 === 4 // true

4 === ‘4’ // false

4. What is scope in JavaScript?

Ans: Scope determines the visibility and accessibility of a variable.

For example:

Function Sum ( first, second) {

Let result = first + second;

Return result

Const output = sum (2, 4);

console.log(output);

console.log(output);

// 6

//result is not defined.

There are three scope in JavaScript: local scope, global scope, block scope.

5. What is closure in JavaScript?

Ans: In JavaScript, a closure is a function that references variables in the outer scope from its inner scope. The closure preserves the outer scope inside its inner scope.

For example:

function makeFunc() {

var name = ‘Mozilla’;

function displayName() {

alert(name);

}

return displayName;

}

var myFunc = makeFunc();

myFunc();

6. What is JavaScript Encapsulation?

Ans: JavaScript Encapsulation is a process of binding the data with the functions acting on that data. It allows us to control the data and validate it.

To achieve an encapsulation in JavaScript: -

  • Use var keyword to make data members private.
  • Use setter methods to set the data and getter methods to get that data.

7. What are call(), apply() and bind() in JavaScript?

Ans: call(): call() method is used to call a function contains this value and an argument provided individually. For example:

function Product(name, price) {

this.name = name;

this.price = price;

}

function Fruits(name, price) {

Product.call(this, name, price);

this.category = ‘fruits’;

}

console.log(new Food(‘mango’, 5).name);

// expected output: “mango”

Apply(): apply() method is used to call a function contains this value and an argument contains elements of an array. For example:

const numbers = [5, 6, 2, 3, 7];

const max = Math.max.apply(null, numbers);

console.log(max);

// expected output: 7

const min = Math.min.apply(null, numbers);

console.log(min);

// expected output: 2

Bind(): bind() method is used to create a new function. When a function is called, it has its own this keyword set to the provided value, with a given sequence of arguments.

8. What is this keyword?

Ans: The this keyword is one of the most widely used and yet confusing keyword in JavaScript. ‘This’ keyword references the object of which the function is a property. In other words, the this references the object that is currently calling the function. In the global context, the this references the global object, which is the window object on the web browser or global object on Node.js.

9. What is global variable?

Ans: Global variable is a variable that declared outside the function or declared with window object. It can be accessed from any function.

10. What is window object?

Ans: The window object is supported by all browsers. It represents the browser’s window. All global JavaScript objects, functions, and variables automatically become members of the window object. Global variables are properties of the window object.

--

--