10 Important JavaScript Q/A

Iffatunnessa
5 min readMay 8, 2021

Part 2

What is the difference between double equal (==) and triple equal (===)?

Double equal (==) compares whether the two variables or values besides it, have same value or not. And triple equal check as same as the double equal and the types of those variables.

Example #1: double equal will check the values and return if it’s true or not

const num1 = 1;
const bool = true;
const num2 = 0;
const bool2 = false;
const num3 = 2;
const string = "2";
if(num1 == bool){
//this block will be executed
}
if(num2 == bool2){
//this block will be executed
}
if(num3 == string){
//this block will be executed
}

Example #2: triple equal will check the values and the type to return if it’s true or not

const num1 = 1;
const bool = true;
const num2 = 0;
const bool2 = false;
const num3 = 2;
const string = "2";
const num4 = 2;
const num5 = 2;
if(num1 === bool){
//this block will not execute
}
if(num2 === bool2){
//this block will not execute
}
if(num3 === string){
//this block will not execute
}
if(num4 === num5){
//this block will be executed
}

What’s the Scopes of JavaScript and what’s the differences between them?

There are two scopes : global and local. Global scopes are declared as global variables which are placed in the upper portion of a function. Local scopes are bounded their access limit with in curly braces.

Let, Constant can have local and global scopes but declared variable with var is a global variable and has global scope into the entire code.

What’s Closer?

A function contains a local variable that can be used with in this function. If we call this function in another function, it’ll make a close environment. That’s called closer.

function countingVariable(){
let count = 0;
count++;
return count;
}
const count1() = countingVariable();
console.log(count1()); //1
console.log(count1()); //2
console.log(count1()); //3
const count2 = countingVariable();
console.log(count2()); //1
console.log(count1()); //4
console.log(count2()); //2

What is DOM?

DOM means Document Object Model. If we want to describe it, we can say that it’s a logical tree structure of HTML of XML documents. DOM contains html structure and style. A node can call multiple leaf nodes or child nodes which have simple structure of it’s own.

Suppose, we need a list of food items in a card(we can take card elements from bootstrap or Material-UI). If we have 100 of food items to show in a single page we have to repeat this card element 100 times. Does that worth it? Isn’t is makes your website too large and costly?

Yes, it does! So, how can we fix it?

Using React components we can make a simple architecture for your card layout and then recall it 100 times for items! That’s it. That’s how DOM and React works with some easy steps.

What’s the different between bind, call and apply?

Let’s say there is an object that has a method in it. And we need another object but that doesn’t have that specific method. If we want to use that method for this new object we’ll use bind.

const calculator = {
count : 0,
summation: function(newCount){
this.count = newCount + 1;
return this.count;
}
}
const count1 = {
count: 1
}
const newCounting1 = calculator.summation.bind(count1); console.log(newCounting1(2)); //3

If we need to call that method for another object we use call and for the same reason will use apply by sending this element within array.

const calculator = {
count : 0,
summation: function(newCount, newCount2){
this.count = newCount + newCount2;
return this.count;
}
}
const count1 = {
count: 1
}
console.log(calculator.summation.call(count1, 3, 2)); //5
console.log(calculator.summation.apply(count1, [3, 2])); //5

What is the keyword “this”?

The keyword this represents an object with in any specific constructor or method. If we specifies the object or class before calling the constructor or method, it’ll refer only that object or class. Otherwise it’ll call that specific method where it has been declared.

How to remove a duplicate element in an array?

For removing duplicate elements from an array, we’ll take another array to store unique elements. Then we’ll check the index value of the elements are equal to -1 or not. If yes, then we’ll add it into the new array, otherwise i’ll skip that element. We can use a for loop which will starts from zero to the length on the first array. We are comparing the index value with -1 because if there is no such values in the new array it’ll be an index with -1.

How to reverse a string?

We have a string that’s gonna be reversed. We need another string to restore the reverse characters. By using for loop, we’ll start from the first index to the last index of this string. If we store the first value into a variable and add it into the empty string it’ll get an array with one character. Then we take another character from the string and add it with the one element string then we got the second letter in reverse. Thus how we can get the reverse string after a full iteration.

What’s the difference between Arrow function and regular function?

Regular functions created using function declarations or expressions are constructible and callable. However, the arrow functions are only callable and not constructible, i.e arrow functions can never be used as constructor functions. Argument objects cannot be found in an arrow function but regular function has that one.

How to find the largest element of an array?

Let’s say we have an array with different integer or float point numbers. At first we need to make a loop for full length of that array. Then, we take a temporary variable to store the first element of that array. After that, we make a condition that if an element is larger than the temporary variable, we’ll assigned that larger value into the temporary one. After one iteration, the temporary variable contains the largest value in it.

Coding demonstrations are coming soon…

--

--