6 Javascript Beginner Level Conceptual Questions Interview Questions & Answers
Below is a list of our Javascript Beginner Level Conceptual Questions interview questions. Click on any interview question to view our answer advice and answer examples. You may view 5 answer examples before our paywall loads. Afterwards, you'll be asked to upgrade to view the rest of our answers.
1. What is hoisting in JavaScript?
This interview question tests the developer's knowledge of JavaScript terminology.
Hoisting is a technique in which the variables (declared using var keyword) and function declarations are moved to the top of their scope before code execution. This means we can access those variables and functions before they are declared in the source code.
This is opposite to C or C++ where one MUST put either function declaration or function definition before they are accessed.
So the below code will run without any error.
a = 2
console.log(a)
var a
myFunc()
function myFunc() {
console.log("inside muFunc")
}
We could access and assign to “a†before its declaration var a and same with function myFunc.
Written by Jennie Taylor on May 21st, 2021
2. What is the difference between var and let?
This question tests the knowledge of JavaScript variables.
var and let both are used to declare variables in Javascript. The var-variables get hoisted while let-variables are not hoisted. We can not access let-variables before they are declared
The variables assigned using var get hoisted to function scope. These variables declared in inner blocks are accessible outside of the block scope. These variables can also be redeclared.
function f() {
a = 2
console.log(a) // 2
{
var a = 4
console.log(a) // 4
}
{
console.log(b) // undefined because it was hoisted
{
// 2-level nested blocks
var b = 13
var b = 3 // redeclaration- OK
}
console.log(b) // 3
}
console.log(b) // 3
}
console.log(a) // error
console.log(b) // error
c = 1
console.log(c) // 1
{
var c = 11
console.log(c) // 11
}
console.log(c) // 11
But, the variables assigned using let are not hoisted and are block level. They can not be redeclared.
{
console.log(a) // error
let a = 4
let a = 5 // redeclaration- error
}
console.log(a) // error
Written by Jennie Taylor on May 21st, 2021
3. What is garbage collection in JS and how does it works?
This interview question concentrates on the developer's knowledge of JavaScript terminology and memory.
The process of freeing the allocated memory by Javascript when that memory is not being used/referenced is known as garbage collection.
In low-level programming languages like C or C++, the memory for objects at run-time is allocated using malloc and free-like functions. It's the programmer’s job to allocate memory for newly created objects and free memory for objects that are no longer used. But in high-level programming languages like Javascript, Python etc the memory management occurs automatically. The interpreter allocates memory automatically and frees it when it finds that memory is not used by anyone.
const array = [1, 2] // #1
const obj = {
prop1: “abcâ€,
myArray: array // #2
}
// ...
// later in the code
obj[array] = undefined // #3
In the above program, the memory is allocated to the array at #1. It is being referenced by other object obj at #2. The array is being referenced by obj (that is, it is being used). So it will not be freed. AT #3, obj no longer references the array, and thus the memory allocated to the array will be released if it was not referenced elsewhere in the program.
Written by Jennie Taylor on May 21st, 2021
4. What is the optional chaining operator in JavaScript?
This question concentrates on JavaScript objects and values.
Optional chaining is a way using which we can access property in deeply nested objects safely even if some nested object is not defined.
Generally, we have deeply nested objects in JavaScript. Since JavaScript is not a type-strict language, there may be invalid properties (properties with value null or undefined). For example in the following object-
{
a1: { b: 12, c: 34 },
a2: { c: 34 },
a3: { b: 12, d: { e: 41 } },
}
In the above object, we have three props a1, a2, a3. Some of these objects have some properties and some don’t. If we access it,
â— a3.d.e, it will return 41,
â— a3.b.e, it will return undefined because a3.b exists but it does not have property “dâ€.
â— a2.d.e, it will through error- “Can not read property ‘e’ of undefinedâ€. This run time error may result in unexpected results.
Here comes the optional chaining operator to rescue. If we access-
1. a3?.d?.e, it will return 41. It will work as if there was no optional chaining operator because all the nested objects are defined.
2. a3?.b?.e, it will return undefined for the same reason as stated above.
3. a2?.d?.e, it will return undefined too. a2 is defined. But there exists no property “d†on a2. So a2?.d will be undefined. Now the optional chaining operator sees we are trying to access some property (“e†here) on an undefined value, it will return undefined straight away.
So while accessing nested properties using the optional chaining operator, if there is an intermediate property that is undefined, the operator will return undefined. It will not try to access the more nested property. Thus avoids the error.
Written by Jennie Taylor on May 21st, 2021
5. What are truthy and falsy values in JavaScript?
This interview question focuses on JavaScript values and terminology.
Falsy: A value that is considered false in a boolean context is a falsy value.
The boolean context may be a if-condition and the logical operators like && (and), || (or), ! (not).
There are exactly 9 values in JavaScript which are falsy-
1. false:- the keyword false
2. 0:- the number zero
3. -0:- the number negative zero
4. 0n:- the bigint zero
5. “â€, ‘’, ``:- the empty string
6. null:- the absence of any value
7. undefined:- the primitive non-defined value
8. NaN:- not a number
9. document.all :- the primitive non-defined value
Truthy: A value that is considered true in a boolean context is a truthy value.
Every value is a truthy value if they are not a falsy value (i.e., except for false, 0, -0, 0n, "", null, undefined, document.all and NaN).
Written by Jennie Taylor on May 21st, 2021
6. What is short circuiting in JavaScript?
This question concentrates on JavaScript operators.
When we chain multiple expressions using && or || operators, the && operator returns the first falsy value and || operator returns the first truthy value while going evaluating expressions from left to right. They don’t evaluate other expressions after the first matched value. This is known as short-circuiting.
One thing to note here- the evaluations of the expressions are done from left to right.
Consider this example using && operator:
const result = val1 && val2 && val3 && val4
While evaluating the whole expression:
◠first, val1 is checked, let say it is truthy value. Since && “finds†the first falsy value, it will continue to the next expression- val2;
â— let us say, val2 is a falsy value. The value of the result will be the value of val2. The values of the expressions val3 and val4 are not calculated. The && operator has short-circuited after evaluating val2.
The same is true of the || operator. The only difference is that it will short-circuit at first truthy value.
One more thing to note here is that if the && operator does not find the first falsy value in the whole chain, it will return the value of the last calculated expression. AND if the || operator does not find the first truthy value in the whole chain, it will return the value of the last calculated expression.
Written by Jennie Taylor on May 21st, 2021