Javascript Fundamental You Need To Know Before Interview

Nazmul Huda
4 min readMay 8, 2021

== vs === ?

In JavaScript, there is confusion about it. What should i use == or ===. But you must know the difference between them. They are not the same thing. If you open your browser console and type

‘10’ == 10 // true; and ‘10’ === 10 // false.

Because, === equal are strictly compare value weather == are not.

Null vs undefined?

Null value and undefined value are explicitly the same. There are 6 undefined values. Undefined can be many ways. But mainly undefined is falsy value.

If you declare a variable and don't pass a value it will undefined, if you declare a function but don't return anything and you want to access the value it will be undefined.

Truthy or Falsy Value?

In Javascript, these are considered falsy …

  • boolean false
  • ” “ empty string
  • 0 number zero
  • NaN — Not a Number
  • undefined
  • null

When placed in an if-branch, it results in a false condition

Everything else is truthy. So that means that some things that seem like they should be falsy are actually truthy.

For example, these all are truthy…

  • {} empty object
  • “0” the string “0”
  • “false” the string “false”

Encapsulation

Encapsulation is one of the main concepts in object oriented programming. It allows an object to group both private and public members under a single name. All the object oriented programming languages support this. Since JavaScript is also an object oriented programming language, it supports it too.

const createCounter = () => {

// A variable defined in a factory or constructor function scope

// is private to that function.

let count = 0;

return ({

// Any other functions defined in the same scope are privileged:

// These both have access to the private `count` variable

// defined anywhere in their scope chain (containing function

// scopes).

click: () => count += 1,

getCount: () => count.toLocaleString()

});

};

const counter = createCounter();

counter.click();

counter.click();

counter.click();

console.log(

counter.getCount()

);

This Keyword

The this keyword in JavaScript has often been a source of much confusion for beginners to the language. Some of this confusion stems from the fact that this in JavaScript is treated differently as compared to in other languages

Functions, in JavaScript, are essentially objects. Like objects they can be assigned to variables, passed to other functions and returned from functions. And much like objects, they have their own properties. One of these properties is this.

let person = {

name: “rimon”,

age: 21,

logInfo : function() {

document.write(this.name + “ is “ + this.age + “ years old “);

}

}

// logs rimon is 21 years old

person.logInfo()

Global Variable

A variable can automatically become global if you set a value to a variable that has not yet been declared. This can happen even if you assign a value to a variable in JavaScript functions:

// code here can use the phone

Function myFunc () {

var phone = ‘my phone’

}

//code here can use phone

Event Loop in Javascript

The Event Loop has one simple job — to monitor the Call Stack and the Callback Queue. If the Call Stack is empty, it will take the first event from the queue and will push it to the Call Stack, which effectively runs it. Such an iteration is called a tick in the Event Loop. Each event is just a function callback.

Recursion

Recursion is best applied when you need to call the same function repeatedly with different parameters from within a loop. While it can be used in many situations, it is most effective for solving problems involving iterative branching, such as fractal math, sorting, or traversing the nodes of complex or non-linear data structures.

Recursion vs Iteration

The Recursion and Iteration both repeatedly execute the set of instructions. Recursion is when a statement in a function calls itself repeatedly. The iteration is when a loop repeatedly executes until the controlling condition becomes false. The primary difference between recursion and iteration is that recursion is a process, always applied to a function, and iteration is applied to the set of instructions that we want to get repeatedly executed.

Arrow Function

Arrow functions have a few important distinctions in how they work that distinguish them from traditional functions, as well as a few syntactic enhancements. The biggest functional differences are that arrow functions do not have their own this binding or prototype and cannot be used as a constructor. Arrow functions can also be written as a more compact alternative to traditional functions, as they grant the ability to omit parentheses around parameters and add the concept of a concise function body with the implicit return.

--

--