Top Ten JavaScript Interview Questions and Answers for beginners

Mohammad Mahbubul Alam
3 min readMay 8, 2021
Designed by vectorjuice / Freepik

Today I describe the top ten JavaScript interview questions and answers for beginners.

1. What are Truthy and Falsy values:

In JavaScript, truthy value’s considered as true in Boolean context, If the values are not true it is called falsy value. Here is a list of some truthy and falsy value-

Truthy Value — ‘0’, ‘ ‘, [], {}, ‘false’Falsy Value — false, 0, “” or ‘’, undefined, null ,NaN

2. Difference Between null and undefined:

A non-existence or empty value is called a null value. A null value is assigned as null. Null means nothing.

Example:

let val = null;console.log(val); //null

When a variable is declared, but a value not assigned is called undefined.

Example:

let val;console.log(val); // undefined

3. Difference Between ‘==’ and ‘===’:

The main difference between double equals(==) and triple equals(===) is Double equals(==) checks only values and triple equals(===) not only check values also check data types.

Example:

const first = 1;const second = ‘1’;if(first == second){console.log(‘Condition is true’); //Condition is true}else{console.log(‘Condition is false’);}if(first === second){console.log(‘Condition is true’);}else{console.log(‘Condition is false’); // Condition is false}

4. What is an API:

API means Application Programming Interface. It’s a set of programming code containing terms, which enables data exchange between one application to another application. Three are three types of API — Private, Shared, Public.

Private API only for use internally, Shared API shared with specific business partners, and Public API available for everyone.

5. What is Closure:

When an inner function has access to the outer function variable, when it is used, then each will have its own value it called closure. It has access to its own scope, the external functions variable, and the global variable.

6. What is the Access Modifiers in JavaScript:

Access modifiers are keywords (public, private, protected) in Object Oriented Programming that specify declared accessibility of classes, method, and property. There are three major access modifiers in JavaScript — public, private, protected

Public: It is available to everyone that can access the (owner) class instance.

Private: It can only accessible within the class that instantiated the object.

Protected: It can access within the class (similar to private) and any object that inherits from it.

7. What is variable scope in JavaScript:

Variable scope is a block of code that determines the accessibility of variables. The two types of variable scope are local and global variables.

Local Variable: If a variable declared inside of a block is called a local variable.

Global Variable: If a variable declared outside of a block is called a global variable.

Example:

var cars = “Toyota”; //Global Variablefunction bike() {var bikeName = “Hero”; //Local Variableconsole.log(bikeName);}console.log(cars);bike();console.log(cars);

8. What is Hoisting:

If you declare a variable with a ‘var’ keyword without value, no matter where variables are declared, they are moved to the top of their scope regardless of whether their scope is global or local, it is called hoisting. But if you declare a variable with a value, you can not use it before the declaration. Hoisting cannot with let and const declaration.

9. What is setTimeout and setInterval:

JavaScript works line by line, but you can change this behavior, if you use setTimeout and setInterval.

Example:

function doSomething(){console.log(2);}console.log(1);setTimeout(doSomething); //This function has been executed at the end of allconsole.log(3);/*Output:132*/

setTimeout() method executes a block of code after the specified time and its executes the code only once.

Example:

setTimeout(function () {console.log(‘Good Morning’)}, 2000) //this function will executes after 2 seconds, it executes only once

setInterval() method executes continuously after a specific time until clearInterval() is called.

setInterval(() => {console.log(‘Doing it’); //this method will executes continuously after 1 sec}, 1000);

10. What is callback function:

When a function sends another function as an argument, it is called a callback function.

Example:

function wellCome(name, message){console.log(name);message();}wellCome(‘Tom Hanks’, function(){ //sends a anonymous function as an argumentsconsole.log(‘Good Morning’);})/*Output:Tom HanksGood Morning*/

--

--