Common interview questions of javascript - Md Iqbal Hossain

Md Iqbal Hossain
4 min readMay 8, 2021

Question 1: What is truthy and falsy value in javascript?

Answer 1: In Boolean context, we know that a value can either be true or false. So in javascript, if the value is considered true that is called truthy value whereas if the value is false that is called falsy value. All values are truthy unless they are defined as falsy (i.e., except for false, 0, -0, 0n, "", null, undefined, and NaN). This sample example can make it clear(which will be coerced to true in boolean contexts, and thus execute the if block):.

Execute if block as truthy values
Execute if block as falsy values

Question 2: Null vs Undefined

Answer 2: Null is a value that can be assigned to a variable on the other hand Undefined is a data type. Let us see this conditional expression of null example

Undefined means a variable is declared but the value is not assigned.

Question 3: = vs == vs === What is the context?

In any programming language if we put = between a variable and value it means the value is assigned to that concerned variable.

== is a comparison operator. It transforms the operands having the same type before comparison. So if we compare a string to a number javascript converts the string into a number first. An empty string is always converted to zero. A string with no numeric value is converted to NaN (Not a Number), which returns false.

=== is a strict equality comparison operator in JavaScript, which returns false for the values which are not of a similar type.

Question 4: Tell us something about scope, block scope, global scope

Answer 4: Scope the visibility or accessibility of a variable or other resource in the area of your code. Block scope is the area within if, switch conditions or for and while loops. Generally speaking, whenever you see {curly brackets}, it is a block. In ES6, const and let keywords allow developers to declare variables in the block scope, which means those variables exist only within the corresponding block.

In the function click() there is an if block where 3 variables are declared by var, const, and let accordingly. Outside the curly braces, they are consol logged and see the result const and let cannot be accessed. Interestingly the variable declared by var can be accessed and can show the output. This is because of a feature called hoisting. If we declare any variable by var it can be lifted up to the parent function and can be accessed from anywhere in the area of the parent function.

Global scope: There’s only one Global scope in the JavaScript document. The area outside all the functions is considered the global scope and the variables defined inside the global scope can be accessed and altered in any other scopes.

Question 5: What is “this”?

Answer 5: “this” is a keyword in javascript. We create objects or methods in our code. this keyword executes it within the context.

Let us see an example

Question 6: Can you Find the largest element of an array?

Answer 6: Yes. See the code below.

Here in the code, there is an array. then we are putting all the elements of the array in a built-in method named Math.max which returns the largest element of an array.

Question 7: Can you reverse a string?

Answer 7: Yes. See the code below.

There are many ways to reverse a string. The method shown above is done by using the built-in method of javascript. At first, we are checking if this is a string or not and if the length is less than 2 then the function will return the input. If it's a string and the length is greater than 2 then the split method will split the string the reverse method will reverse the string and finally join method will join the string. All the methods are connected by method chaining.

--

--