Masud Rana

May 8, 2021

4 min read

javaScript interview questions you need to know

JavaScript is one of the most popular languages for web development.
It was initially created to develop dynamic web pages. Every JS program is called a script, which can be attached to any web page’s HTML. These scripts run automatically when the page loads. A language which was initially used to create dynamic web pages can now be executed on the server and practically on any device consisting of the JavaScript Engine.

We are going to learn JavaScript, by answering the most frequently asked javascript interview questions:

In JavaScript, a truthy value is a value that is considered true when encountered in a Boolean context. All values are truthy unless they are defined as falsy

‘0’

‘ ’

[]

A falsy (sometimes written falsey) value is a value that is considered false when encountered in a Boolean context. JavaScript uses type conversion to coerce any value to a Boolean in contexts that require it

0

“ ”

Undefined

Null

NaN

In JavaScript, undefined is a type, whereas null an object. It means a variable declared, but no value has been assigned a value. Whereas, null in JavaScript is an assignment value.

triple equals ( === ) will do the same comparison as double equals (including the special handling for NaN, -0, and +0 ) but without type conversion; if the types differ, false is returned.

=== Strict Equality Comparison (“strict equality”, “identity”, “triple equals”)

== Abstract Equality Comparison (“loose equality”, “double equals”)

map calls a provided callbackFn function once for each element in an array, in order, and constructs a new array from the results. callbackFn is invoked only for indexes of the array which have assigned values (including undefined).

The find() method returns the first value that matches from the collection. Once it matches the value in the findings, it will not check the remaining values in the array collection. The filter() method returns the matched values in an array from the collection.

A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.

In JavaScript there are two types of scope:

Local scope

Global scope

JavaScript has function scope: Each function creates a new scope.

Scope determines the accessibility (visibility) of these variables.

Variables defined inside a function are not accessible (visible) from outside the function.

A JavaScript global variable is declared outside the function or declared with window object. It can be accessed from any function.

Let’s see the simple example of global variables in JavaScript.

var value=50;//global variable

function a(){

alert(value);

}

function b(){

alert(value);

}

JavaScript Objects —

A JavaScript object is an entity having state and behavior (properties and method). For example car, pen, bike, chair, glass, keyboard, monitor, etc.

JavaScript is an object-based language. Everything is an object in JavaScript.

JavaScript is template based not class-based. Here, we don’t create a class to get the object. But, we direct create objects.

Creating Objects in JavaScript

There are 3 ways to create objects.

  1. By object literal
  2. By creating instance of Object directly (using new keyword)
  3. By using an object constructor (using new keyword)

JavaScript Array —

JavaScript array is an object that represents a collection of similar type of elements.

There are 3 ways to construct array in JavaScript

  1. By array literal
  2. By creating instance of Array directly (using new keyword)
  3. By using an Array constructor (using new keyword)

JavaScript Encapsulation —

JavaScript Encapsulation is a process of binding the data (i.e. variables) with the functions acting on that data. It allows us to control the data and validate it. To achieve 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.