Javascript random Interview Questions

Mahmudul Hasan
4 min readMay 8, 2021
Photo by James Harrison on Unsplash

In order to get a job in any company we all have to attend interviews. Getting a Web developer job is not beyond it. If you are a new javascript developer and looking for internship jobs, well here is some most common interview questions that may help you.

1 null vs undefined

This is a very common interview question for an intern’s interview. Well, let’s try to figure it out.

null means nothing , empty. It means there was something previously but there is nothing now. But to get null we have to set a null value .

undefined can be find many other way. In bellow you can see some example how to get the value undefined.

// Undefined// 1:
let name;
console.log(name);
// 2
// If the function returns nothing
function add(num1, num2) {console.log(num1+ num2);return(num1 + num2);}const result = add(1, 2);
console.log(result);
// 3
const loverBoy = { name: ‘likerLotik’, age:20}
console.log(loverBoy.gf);
// 4
let myBrain = undefined;
console.log(myBrain);

2: Double vs Triple equal (== vs ===)

== checks the value while === checks the value and the type both. the best practice is to use the === every time.

const first = 2;
const second = ‘2’;
if (first == second) {
console.log(‘hiiiiiiii’);
}
else{
console.log(‘byeeeeeeeee’);
}
// will return hiiiiii

3: find() vs filter()

find returns the first item of matching items from the object. ON the other hand filter returns all the items matches with the condition.

const students = [{ name: ‘ola’, age:40},{ name: ‘koli’, age:52},{ name: ‘bisu’, age:60},];const big = students.filter(s => s.age>50);console.log(big);const bigger = students.find(s => s.age> 40);console.log(bigger);

4 Global & Local Scoop, Hoisting

Global variable is something which is accessible form everywhere but local scoop or variable is only accessible from the loop where it is declared.

hoist a variable declaration and a function declaration to the top of a function scope. Those variables and functions can be used from any part of the function without raising no reference Exception. “var” always hoists, its value can be accessed from any part.

//global variablevar a = 2;//global functionfunction b(){console.log(a); //access global variable}b(); //execute global function.// Localfunction sum(first, second){let num = first + second;console.log(num);if(num > 8){let myMood = “Goodbye”;console.log(myMood);}// console.log(myMood); // can’t be accessedreturn num;}const result = sum(4,9)console.log(result);// console.log(myMood); // can’t be accessed// console.log(num); // can’t be accessed

5: What is bind()

By bind method we can bind an object to a common function, so that the function gives different result when its needed.

bind method takes an object as an first argument and creates a new function.

const student = {name:’bili’,id: 20,about: function() {console.log(`her name is ${this.name} and id ${this.id}`);}}const student2 = {name:’mili’,id: 12}const student3 = {name:’eili’,id: 32}student.about()// bindlet otherStudent = student.about.bind(student2)otherStudent()

more exalmple

const ourHero = {name: ‘KIKI’,netWorth: 40000,finalWorth: function(taxBill){this.netWorth= this.netWorth — taxBill;return this.netWorth;}}ourHero.finalWorth(10000),console.log(ourHero.netWorth);const ourHero2 = {name:’CoCO’,netWorth: 34000}const ourHero3 = {name:’MacO’,netWorth: 59900}// bind()let ourHero2Worth = ourHero.finalWorth.bind(ourHero2);console.log(ourHero2Worth(2000));let ourHero3Worth = ourHero.finalWorth.bind(ourHero3);console.log(ourHero2Worth(4500));

6 what are the scopes of a variable?

There are two scopes:

  • Global Variables: A global variable has a global scope meaning that it will be visible everywhere within your code.
  • Local Variables: Local variables will only be visible within the function in which it has been defined.

7 What does the ‘This’ operator in JavaScript do?

The ‘This’ keyword used in JavaScript talks about the object which it belongs to. It has many different values and it depends on where it is being used.

8 What is Closure?

Closure is developed when a specific variable is defined outside the current scope and it is accessed from within with some inner scope.

function init() {
var name = 'xyz'; // name is a local variable created by init
function displayName() { // displayName() is the inner function, a closure
alert(name); // use variable declared in the parent function
}
displayName();
}
init();

9 What are some of the built-in methods in JavaScript ?

Concat() helps to join two or more than two stringsCharAt() helps to return the character at the specific indexforEach() helps to call a function for each element present in the arraylength() helps to return the length of the stringindexOf() helps in returning the index within the calling String object of the first occurrence of the specific valuepush() helps to add one or more than one element to the end of an array and then return the new length of that arraypop() helps to remove the last element from an array and return that elementreverse() helps to reverse the order of elements of an array

10 How can you create a cookie with the help of JavaScript?

You can create a cookie in JavaScript simply by assigning a string value to the document.cookie object.The syntax:document.cookie = “key1 = value1; key2 = value2; expires = date”;

--

--