Some Common Interview Questions in JavaScript.

Md. Nazmul Hasan
4 min readMay 8, 2021

Truthy and Falsy

In JavaScript, a truthy value is a value that is considered true when coming across in a Boolean context. A falsy value is a value that is considered false when coming across in a Boolean context

Truthy: if — (true), ({}), ([]), (“0”), (“false”)

Falsy: if — (false), (null),(undefined), (0),(NaN)

Undefined vs Null

In JavaScript, Undefined is a type, on the other hand, null is an object.

Undefined means when a variable is declared but no value is assigned. Null is an assignment value that we can assign to a variable.

Undefined:

var demo;alert(demo); //shows undefinedalert(typeof demo); //shows undefined

Null:

var demo = null;alert(demo); //shows nullalert(typeof demo); //shows object

Different ways to get undefined

  • If we don’t define a variable.
  • If we don’t return explicitly in the function that means we write return then don’ write anything.
  • If we don’t pass a parameter.
  • If we write a variable value as undefined we get an undefined value.
  • After declaring an object then if we want to access another property as output but it is not in this object.

Double equals (==) vs Triple equals (===)

Double equals (==) checks for value equality only. It inherently does type coercion. This means that before checking the values, it converts the types of the variables to match each other. On the other hand, Triple equals (===) does not perform type coercion. It will verify whether the variables being compared have both the same value and the same type.

const number = 4356const stringNumber = ‘4356’console.log(number == stringNumber) //trueconsole.log(number === stringNumber) //false

Scope

Scope in JavaScript refers to the current context of code, which determines the accessibility of variables to JavaScript. Two types of scope are: global and local.

Global variables are those that declared outside of a block. Local variables are those that declared inside of a block.

var x = “declared outside function”;outsideFunction();function insideFunction() {console.log(“Inside function”);console.log(x);}console.log(“Outside function”);console.log(x);

Block Scope

Block scope is an area within if, switch conditions or for and while loops. When we see curly brackets { } it’s a block. In ES6 edition, const and let keywords allow developers to declare variables in the block scope, which means variable exists only within the corresponding block.

function fruits(){if(true){var fruit1 = ‘apple’; //exist in function scopeconst fruit2 = ‘banana’; //exist in block scopelet fruit3 = ‘strawberry’; //exist in block scope}console.log(fruit1);console.log(fruit2);console.log(fruit3);}fruits();

Closure

Clouser means that an inner function always has access to the vars and parameters of its outer function, even after the outer function has returned. Clouser is one of the most important concepts in javascript, it is a widely discussed but still confused concept.

function OuterFunction() {var outerVariable = 100;function InnerFunction() {alert(outerVariable);}return InnerFunction;}var innerFunc = OuterFunction();innerFunc(); // 100

Encapsulation

JavaScript Encapsulation is a process of binding the data that are variables with the functions acting on that data. It allows us to control the data and validate it. To achieve encapsulation in JavaScript we have to:

- use the var keyword to make data members private,

- use the setter method to set data, and the getter method to get data.

Difference between bind, call, and apply

Bind: returns a new function, allowing you to pass any number of arguments.

var employee1 = {firstName: ‘John’, lastName: ‘Rodson’};var employee2 = {firstName: ‘Jimmy’, lastName: ‘Baily’};function invite(greeting1, greeting2) {console.log(greeting1 + ‘ ‘ + this.firstName + ‘ ‘ + this.lastName+ ‘, ‘+ greeting2);}var inviteEmployee1 = invite.bind(employee1);var inviteEmployee2 = invite.bind(employee2);inviteEmployee1(‘Hello’, ‘How are you?’); // Hello John Rodson, How are you?inviteEmployee2(‘Hello’, ‘How are you?’); // Hello Jimmy Baily, How are you?

Call: The call() method invokes a function with a given value and arguments provided one by one.

var employee1 = {firstName: ‘John’, lastName: ‘Rodson’};var employee2 = {firstName: ‘Jimmy’, lastName: ‘Baily’};function invite(greeting1, greeting2) {console.log(greeting1 + ‘ ‘ + this.firstName + ‘ ‘ + this.lastName+ ‘, ‘+ greeting2);}invite.call(employee1, ‘Hello’, ‘How are you?’); // Hello John Rodson, How are you?invite.call(employee2, ‘Hello’, ‘How are you?’); // Hello Jimmy Baily, How are you?

Apply: Invokes the function with a given this value and allows you to pass in arguments as an array.

var employee1 = {firstName: ‘John’, lastName: ‘Rodson’};var employee2 = {firstName: ‘Jimmy’, lastName: ‘Baily’};function invite(greeting1, greeting2) {console.log(greeting1 + ‘ ‘ + this.firstName + ‘ ‘ + this.lastName+ ‘, ‘+ greeting2);}invite.apply(employee1, [‘Hello’, ‘How are you?’]); // Hello John Rodson, How are you?invite.apply(employee2, [‘Hello’, ‘How are you?’]); // Hello Jimmy Baily, How are you?

Call and apply are pretty interchangeable. Both execute the current function immediately. You need to decide whether it’s easier to send in an array or a comma-separated list of arguments. You can remember by treating Call as for comma (separated list) and Apply as for Array. Whereas Bind creates a new function that will have this set to the first parameter passed to bind().

Features of JavaScript

JavaScript is one of the most popular languages which includes numerous features when it comes to web development. Some of the features are lightweight, dynamic, functional, and interpreted. So, some important features of javascript are:

  • Light Weight Scripting language
  • Dynamic Typing
  • Object-oriented programming support
  • Functional Style
  • Platform Independent
  • Prototype-based
  • Interpreted Language
  • Async Processing
  • Client-Side Validation
  • More control in the browser

Arrow Function

Arrow function is one of the features introduced in the ES6 version of JavaScript. It allows creating functions in a cleaner way compared to regular functions.

Function

// function expressionlet x = function(x, y) {return x * y;}

Arrow Function

// using arrow functionslet x = (x, y) => x * y;

--

--