JavaScript, Crack the Interview

Sakibuddin
4 min readMay 10, 2021

--

An interview is a part and parcel of the developer. This is the field of proving yourself that you are the right person for the recruiters. They check the inner view of the Candidates in an interview. To help you in this battle-ground, here I have discussed some tricky concept of JavaScript -

Equality Operators

In JS, there are two ways to check the equality of two values.

Equality Operator (==):
Equality operator does type coercion before comparing the operands which result in weird output and performance issues.

'1'  ==  1         // true
1 == '1' // true
0 == false // true
0 == null // false

Strict Equality Operator (===):
The strict equality operator compares the operands without any type of type coercion.

3 === 3   // true
3 === '3' // false

undefined

undefined is a property of the global object with an initial value undefined. In modern browsers, it is a non-configurable and non-writable property. We can get undefined for the following cases:

  • accessing the global variable undefined
  • accessing a declared but not yet initialized variable
  • calling functions without a return statement
  • return with nothing to return
  • non-existent object properties
  • function parameters without any explicit value

undefined means a variable has been declared, but the value of that variable has not yet been defined.

var name; 
console.log(name);

Expected output: undefined

undefined is of the type undefined

var name; 
console.log(typeof (name));

Expected output: undefined

NULL

null means an empty value. null has been allocated and clearly implies nothing.

console.log(null);

Expected output: null

null is an object

console.log(typeof (null));

Expected output: object

Hoisting

Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution. This means that no matter where functions and variables are declared, they are moved to the top of their scope regardless of whether their scope is global or local.

a = 5;
console.log(a); // Error
b = 5;
console.log(b); // 5
var b;

Object Oriented Programming

Object oriented programming, commonly known as OOP, is a style of programming where a program is broken into several segments of objects that can communicate with each other. Each object may have its own set of properties and methods. There are 3 core concepts ofOOP in JavaScript.

  1. Encapsulation
  2. Polymorphism
  3. Inheritance

Encapsulation:

Encapsulation is Binding the data with the code that manipulates it, It keeps the data and the code safe from external interference.

Inheritance:

Inheritance is the mechanism by which an object acquires the some/all properties of another object. It supports the concept of hierarchical classification.

Polymorphism:

Polymorphism means processing objects differently based on their data type or class. In other words, it means one method with multiple implementations, for a certain class of action. And which implementation to be used is decided at runtime depending upon the situation (i.e., the data type of the object).

Call Stack

In JavaScript, a call stack is a data structure that uses the Last In, First Out (LIFO) principle to temporarily store and manage function invocation (call).

function firstFunction(){
throw new Error('Stack Trace Error');
}
function secondFunction(){
firstFunction();
}
function thirdFunction(){
secondFunction();
}
thirdFunction();
//Stack Trace Error! But it also represents the corresponding call //stack
// at firstFunction (<anonymous>:2:11)
// at secondFunction (<anonymous>:5:5)
// at thirdFunction (<anonymous>:8:5)
// at <anonymous>:10:1

Higher-Order Functions

In JavaScript, functions that accept callback functions as their arguments, are called higher-order functions. For example, map, filter, and reduce are examples of built-in higher-order functions in JS.

const numbers = [1,2,3,4,5];
const total = numbers.reduce((a,b)=>a+b,0);
console.log(total) // 15

JavaScript Scope

Scope determines the accessibility (visibility) of variables. In JavaScript there are two types of scope:

  • Local scope
  • Global scope

Local JavaScript Variables

Variables declared within a JavaScript function, become LOCAL to the function. Local variables have Function scope: They can only be accessed from within the function.

Global JavaScript Variables

A variable declared outside a function becomes GLOBAL. A global variable has global scope. All scripts and functions on a web page can access it.

Let Declarations (let)

The let declaration syntax is the same as the syntax for var. You can basically replace var with let to declare a variable, but limit the variable’s scope to only the current code block.

Constant Declarations (const)

You can also define variables in ES 6 with the const declaration syntax. Variables declared using const are considered constants, meaning their values cannot be changed once set.

Closure

The closure is simply the ability of a function at the time of declaration to remember the references of variables and parameters on its current scope, on its parent function scope, on its parent’s parent function scope until it reaches the global scope with the help of Scope Chain. Basically, it is the Scope created when the function was declared.

Best of Luck!!!

--

--

Sakibuddin

I am Sakib Uddin from Bangladesh. I am a front End Developer