10 Interview questions every javascript developer should know.

JavaScript

Truthy-falsy

All the value we declare has a boolean context of true and false, when we declare those variable with such context we will get either truthy value or falsy value. All the values are truthy unless they are defined falsy.

if (false)
if (null)
if (undefined)
if (0)
if (-0)
if (0n)
if (NaN)
if ("")
if (true)
if ({})
if ([])
if (42)
if ("0")
if ("false")
if (new Date())
if (-42)
if (12n)
if (3.14)
if (-3.14)
if (Infinity)
if (-Infinity)

Null vs. Undefined

Many of us get confused with null and undefined keywords but this two are far different from each other and very easy to understand. Let me explain.

let a = null;
console.log(a);
// output: null
let b;
console.log(b);
// output: undefined

Double Equals vs. Triple Equals

JavaScript has different, ways to test equality. You can test equality with == or ===. Here are the differences:

Triple Equals

When using triple equals === in JavaScript, it always test for strict-equality This means it will check both type and the value of the variable.

5 === 5
// true
77 === ‘77’
// false

Double equals

When using double equals in JavaScript we are testing for loose equality. Double equals also perform type coercion.

77 == ‘77’
// true

Scope

In javascript, every function is a scope. Scope determines from where you can access a variable. There are two types of scope

  1. Global Scope

Local Scope

Variable declared inside a function is local scope. For example :

// code here can not use name variable function myFunction() {
var name= “name”;
// code here can use name variable}

Global Scope

In the global scope, the variable is declared outside a function and can access inside a function. For Example:

var name= “name”;// code here can use name variablefunction myFunction() { // code here can also use name variable}

Closure

A closure is a feature in JavaScript where an inner function has access to the outer (enclosing) function’s variables.

  • it has access to the outer function’s variables
  • it has access to the global variables.
function outer() {   var b = 10;
function inner() {

var a = 20;
console.log(a+b);
}
return inner;
}

What is “this” keyword in JavaScript?

this keyword refers to an object, that object which is executing the current bit of javascript code.

function bike() {
console.log(this.name);
}
var name = “Ninja”;
var obj1 = { name: “Pulsar”, bike: bike };
var obj2 = { name: “Gixxer”, bike: bike };
bike(); // “Ninja”
obj1.bike(); // “Pulsar”
obj2.bike(); // “Gixxer”

call(), bind() & apply() methods

.call()

This method is used to call a function with a given value & arguments. Know that when we invoke a function in JavaScript like functionName(), under the hood, it actually works like functionName.call(). Here, we’ll be using .call() method to borrow the salaryHike() method from person1 & use it for person2. Sounds exciting? Let’s see -

let person1 = {
name:'Unmoy Biswas',
age:22,
salary:20000,
salaryHike(){
this.salary += 10000
}
}
let person2 = {
name:'Utsha Biswas',
age:22,
salary:20000,
}
console.log(person1.salary)
person1.salaryHike.call(person2)
console.log(person2.salary)
// 20000// 30000
let person1 = {
name:'Unmoy Biswas',
age:22,
salary:20000,
salaryHike(hike){
this.salary += hike
}
}
let person2 = {
name:'Utsha Biswas',
age:22,
salary:20000,
}
console.log(person1.salary);
person1.salaryHike.call(person2, 20000);
console.log(person2.salary);
//20000//40000

.apply()

Now, let’s try to implement the same example by replacing .call() method with .apply().

let person1 = {
name:'Unmoy Biswas',
age:22,
salary:20000,
salaryHike(hike){
this.salary += hike
}
}
let person2 = {
name:'Utsha Biswas',
age:22,
salary:20000,
}
console.log(person1.salary);
person1.salaryHike.apply(person2, 20000);
console.log(person2.salary);
let person1 = {
name:'Unmoy Biswas',
age:22,
salary:20000,
salaryHike(hike){
this.salary += hike
}
}
let person2 = {
name:'Utsha Biswas',
age:22,
salary:20000,
}
console.log(person1.salary);
person1.salaryHike.apply(person2, [20000]);
console.log(person2.salary);
//20000//40000

.bind()

Now, let’s implement the same example by replacing .apply() method with .bind(). Also, we shall be passing the argument in the same way as we did in the case of .call() method, like,

let person1 = {
name:'Unmoy Biswas',
age:22,
salary:20000,
salaryHike(hike){
this.salary += hike
}
}
let person2 = {
name:'Utsha Biswas',
age:22,
salary:20000,
}
console.log(person1.salary);
person1.salaryHike.bind(person2, 20000);
console.log(person2.salary);
let person1 = {
name:'Unmoy Biswas',
age:22,
salary:20000,
salaryHike(hike){
this.salary += hike
}
}
let person2 = {
name:'Utsha Biswas',
age:22,
salary:20000,
}
console.log(person1.salary);
let hikeperson2 = person1.salaryHike.bind(person2, 20000);
hikeperson2();
console.log(person2.salary);
//20000//40000

What is the DOM? ( Document Object Model )

When a web page is loaded, the browser creates a Document Object Model of the page. With the DOM, JavaScript can access and change all the elements of an HTML document. The DOM is an object-oriented representation of the web page, which can be modified with a scripting language such as JavaScript.

What is JavaScript?

JavaScript is a dynamic computer programming language. It is lightweight and most commonly used as a part of web pages, whose implementations allow client-side script to interact with the user and make dynamic pages. It is an interpreted or just-in-time compiled programming language with object-oriented capabilities.

// function
function greet(name, callback) {
console.log('Hi' + ' ' + name);
callback();
}

// callback function
function callMe() {
console.log('I am callback function');
}

// passing function as an argument
greet('Unmoy', callMe);
// Hi Unmoy
// I am callback function

Web Designer