Most Common JavaScript Interview QnA You Must Know!

Zahirul Islam Akash
4 min readMay 7, 2021
JavaScript Interview Question

WHAT IS TRUTHY VALUE OR FALSY VALUE?

Truthy value or Falsy is a value what is cast in boolean. When we use if else, if and else are work as a boolean like Yes or No.

if your values are ‘false’, 0, ‘’ ‘’, NaN, null or undefined its a falsy value. Else its truthy value.

WHAT IS ‘this’ KEYWORD?

‘This’ is one of the most tricky and confusing topics in JavaScript. This is a reserved keyword in JavaScript, meaning this name can no longer be used for any variables or functions. And this is used in many ways in many places. The value of this is usually determined based on how a function is being called. And its value is determined at the time of execution.

BIND( ) CALL( ) AND APPLY( )

Javascript all kind of functions are a object. That Means, they can have properties and methods like any other object.

Functions are a special type of object in that they come with a variety of built-in properties and methods, three of which are call(), apply(), and bind().

BIND( )

Whatever object we pass as an argument to .bind() becomes what the ‘this’ variable points to when the function is ran. Example:

var person = {  
firstname: 'Zahid',
lastname: 'Hossain',
getFullName: function() {
var fullname = this.firstname + ' ' + this.lastname
return fullname
}
}
var logName = function(lang1, lang2){ console.log('Person Name: ' + this.getFullName())
console.log('Person Language: ' + lang1 + ' ' + lang2)
}
var logPersonName = logName.bind(person)logPersonName('en', 'bn');

We can only call bind() on a function once. Trying to use it subsequently will not do anything.

CALL( )

call() is similar to bind() in that it allows you to pass in what you’d like a ‘this’ value to point to. Except call() doesn’t make a copy, it invokes functions immediately. example:

var person = {  
firstname: 'Zahid',
lastname: 'Hossain',
getFullName: function() {
var fullname = this.firstname + ' ' + this.lastname
return fullname
}
}
var logName = function(lang1, lang2){console.log('Person Name: ' + this.getFullName())
console.log('Person Language: ' + lang1 + ' ' + lang2)
}
var logPersonName = logName.bind(person)logPersonName('en', 'bn');

Is it look like same? it’s not same at all. We’re given the same return value as with bind() above, except this time we didn’t make a copy and call the function immediately.

APPLY( )

Apply() does the exact same thing as call(), but it requires the original function’s arguments to be passed in as an array.

logName.apply(person, 'en', 'es')it will return an error
logName.apply(person, ['en', 'es']);
it it correct.

CALLBACK FUNCTION

The callback function is one of those concepts that every JavaScript developer should know. Callbacks are used in arrays, timer functions, promises, event handlers, and much more.

The callback function is supplied as an argument to a higher-order function that invokes (“calls back”) the callback function to perform an operation.

function greet(name) {
return `Hello, ${name}!`;
}

greet('Cristina');
const persons = ['Farhan', 'Akash'];

const messages = persons.map(greet);
messages;

CLOSURE()

Closures are one of the key concepts of JavaScript and allow developers to write better code. A closure is a function which has access to the variable from another function’s scope. This is accomplished by creating a function inside a function. Of course, the outer function does not have access to the inner scope.

Example:

function buildName(name) {var greeting = "Hello, " + name + "!";var sayName = function() {var welcome = greeting + " Welcome!";console.log(greeting);};return sayName;}var sayMyName = buildName("Farhan");sayMyName();  // Hello, Farhan. Welcome!sayMyName();  // Hello, Farhan. Welcome!sayMyName();  // Hello, Farhan. Welcome!

NULL VS UNDEFINED

Undefined is a falsy value which means there is nothing that you exactly want. If you declare a variable and not set a value, it will be undefined.

let myName;
console.log(myName);
it will return undefined

Because, you declare a variable but not set the value. Undefined is a reserved kewword and its a falsy value.

Null is also a falsy value, that this is not exist or empty.

let name = null;console.log(name);it will return null

that means name is doesn’t exist.

DOUBLE VS TRIPLE EQUAL (== VS ===)

== is used for comparing two variables, but it ignores the datatype of variable. === is used for comparing two variables, but this operator also checks datatype and compares two values. Checks the equality of two operands without considering their type. Example:

'21' == 21
its true
'21' === 21
it false

--

--