Introduction
JavaScript is one of the most popular and widely used programming languages in the world. It is used on both client-side and server-side. It helps in developing websites like web applications, mobile applications, and other platforms etc. For fresher’s starting their career in web development, having a strong grasp of JavaScript fundamentals is crucial.
In interviews, employers often assess a candidate’s understanding of core concepts, problem-solving skills, and ability to write clean, efficient code.
In this article, we will discuss the top 10 JavaScript interview questions for a fresher with a step-by-step guide:
1) What is a NaN() function?
The NaN() function helps to find whether the given value is not a number. Type of NaN() returns that a “Number”. If the given parameter is a number, returns true; otherwise, returns false.
Example:
let a = 20/0;
let b=0/0;
let d=typeof a; // checks that the given value is a number or not
console.log(d); // Output: number
let f=isNaN(b) // if the value is a valid number, return true
console.log(f); return true
console.log(isNaN(10)); return false because 10 is a number.
2) Explain the var, let, and const keywords
Var: Before coming ES6 version, variables were declared using the var keyword. It has a function and a global scope variable. It allows re-assignment and re-declaration of a value.
let: After coming ES6 version, variables are declared using the let keyword.
It has a block-scope variable. It does not allow re-assignment or re-declare a value.
const: The value of a constant variable cannot be updated. After coming ES6 version, variables are declared using the let keyword. It has a block-scope variable. It does not allow re-assignment or re-declaration of a value.
3) Explain == and === Operators in details
Equality Operator (==) is also known as the loose equality. It's only used to compare the value, not the type.
Strict Equality Operator (===) is used to compare values and types.
console.log(2 == 2); return true because both values are same
console.log(2 === '2'); return false because number and string both are different.
4) Explain Null, undefined, and undeclared in detail
Null:
It stores the primitive value. Because it is a primitive data type. In Null, we need to assign the null value to the variable. If you are assigning null value to the variable, it shows that the variable does not hold any value. The type of null is the “Object”.
let name = null;
console.log(name); // output: null
Undefined:
It represents that the variable has been declared but has not been given a value. The type of undefined is also undefined.
let a;
console.log(a); // output- undefined
console.log(typeof a); // output- undefined
undeclared:
It represents that the variable has not been declared or initialized using the let, var, or const keyword.
console.log(data); // ReferenceError: data is not defined
5) Explain different data types in JavaScript
In JavaScript, data types are divided into two types such as primitive and non-primitive.
Primitive data types: They store a single and immutable value.
· Number (integer and floating value, e.g., 15, 5.02)
· String (text data in single or double quotes, e.g., 'tpoint' ,"tech")
· Boolean (true or false(default) value)
· BigInt (large integer value, e.g., 954154154815)
· Null
· Undefined
· Symbol
Non-primitive data types: They collect groups of data and mutable values.
· Array (store multiple values in a signal variable, e.g. [10, 20, 30])
· Object (store key-value pairs, e.g. {Name: ‘Moto’ , Model: 2024}
· Function (block of code e.g. function sum (x=5, y=15) {return a*b}).
6) Explain the Scope of a variable in JavaScript.
The scope of a variable in JavaScript represents the accessibility of the variable in a program.
o Global scope: In this scope, you declare the variable outside the function and access the variable anywhere in the program.
o Block scope: You cannot access the variables from outside the block, and also cannot declare them.
o Function Scope: In this scope, you cannot access the variables from outside the function.
7) Explain hoisting in JavaScript
In JavaScript, hoisting is a JavaScript’s default behavior of moving declarations to the top of their scope before code execution. Only you can declare the parts that are hoisted, but not the initializations. The scope of a function and variable can be local or global.
data = 10;
console.log(data); // output: 10
var data;
8) Explain template literals in JavaScript.
It is also referred to the template strings, which were introduced in ES6. It allows you to embed expressions or variables in your code. Instead of single or double quotes, you can use the backticks(`` ` ``).
let name = "John";
let address = 16;
console.log( `Hello, my name is ${name}, and I live in Noida ${address}. `);
Output: Hello, my name is John, and I live in Noida 16.
9) Explain Closures in JavaScript.
Closures help to create private variables or functions. With the use of closures, you can easily access the inner function from the outer function variables after the function has been executed it.
function outer(outer) {
function inner() {
console.log(outer);
}
return inner;
}
let Closure = outer("Hello from outer function!");
Closure(); //Output: Hello from outer function!
10) Explain map(), filter() and reduce() methods in details.
These are the methods used in higher-order functions. Explain in detail below:
map(): This method helps to create a new array by applying a condition. But the original array remains the same.
let arr = [1, 2, 3, 4];
let Num = arr.map(arr => arr * 2);
console.log(Num); // Output: [ 2, 4, 6, 8 ]
filter(): This method also helps to create a new array containing only those elements that satisfy the given condition. But the original array remains the same.
let arr = [5, 20, 15, 40];
let evenNum = arr.filter(arr => arr % 2==0);
console.log(evenNum); // Output: [ 20, 40 ]
reduce(): This method helps to reduce an array into a single data.
let num = [1, 2, 3, 4];
let sum = num.reduce((acc, curVal) => acc + curVal, 0);
console.log(sum); //Output: 10
Conclusion
This article covers 10 JavaScript interview questions for fresher. JavaScript has one of the most in-demand skills for full-stack developers, and covers core concepts for interview preparation. These questions are not theory type, but they help in real-life scenarios.
I recommend that you learn JavaScript programming from the Tpoint tech website, as it provides JavaScript Tutorials, interview questions, and a Compiler as well.