JavaScript Interview Questions and Answers For Freshers
JavaScript Interview Questions and Answers For Freshers
Java and JavaScript are both programming languages, but they have
different origins, syntax, and use cases.
2) What is JavaScript?
• Object-Oriented
• Dynamic Typing
• Interactivity
• Cross-Platform
• Client-Side Execution
• Easy to Learn
• Versatility
Advantages
• Fast speed: JavaScript is executed on the client side that’s why it is very
fast.
• Easy to learn: JavaScript is easy to learn. Any one who have basic
knowledge of programming can easily lean JavaScript.
• Server Load: It reduces the server load because it executes on the client
side.
Disadvantages:
const person = {
firstName: "Raju",
lastName: "WebDev",
function myFunction() {
// write code here
}
call(), apply(), and bind() are methods available in JavaScript which allows
you to change the context in which a function is executed.
i) call() method is used to invoke a function with a specified this value and
arguments provided individually. It accepts the this value as the first
argument, followed by the arguments to be passed to the function.
function greetHello(name) {
console.log(`Hello, ${name}!`);
}
const person = {
name: 'Geeks Help'
};
greetHello.call(person.name, person.name);
const person = {
name: 'Raju'
};
const person = {
name: 'Raju WebDev'
};
function greetHello() {
console.log(`Hello, ${this.name}!`);
}
greetHelloToRaju();
regularExpressionObj.exec(string);
function add(x) {
return function (y) {
return x + y;
}
}
// Usage
const add5 = add(5);
console.log(add5(3));
//Output: 8
console.log(add5(7));
//Output: 12
Closures are the functions which refer to independent variables. In other words,
the function defined in the closure 'remembers' the environment in which it
was created.
function numberGenerator() {
// Local “free” variable that ends up within the closure
let num = 1; function checkNumber() {
console.log(num);
}
num++;
return checkNumber;
}
// Output: 2
To read a cookie using JavaScript, you can use the document.cookie property,
which returns a string containing all the cookies associated with the current
document.
To delete a cookie using JavaScript, you can set its expiry date to a date in the
past. This will cause the cookie to expire immediately, effectively deleting it.
The word "debugger" in JavaScript refers to the built-in debugging tool which
is available in most modern web browsers. This tool allow developers to pause
the execution of their JavaScript code, examine the state of variables and
objects, and step through the code line-by-line to find and fix errors or bugs
in their code.
In JavaScript, var and let both are used to declare variables, but they have
some important differences in how they work:
ii) let is block-scoped, which means that a variable declared with let is only
accessible within the block. (i.e. between the curly braces {}) it was declared
in, including any nested blocks.
2). Hoisting:
i) Variables declared with var are hoisted to the top of their scope, which
means that they can be accessed before they are declared, but their value will
be undefined until they are assigned a value.
ii) Variables declared with let are not hoisted, which means that they cannot
be accessed before they are declared.
3). Re-assignment:
i) Variables declared with var can be re-assigned to a new value within their
scope.
ii) Variables declared with let can also be re-assigned, but they cannot be re-
declared within the same scope.
When two values of different data types are compared using the == or !=
operators, JavaScript will implicitly convert one or both of the values to a
common data type before making the comparison.
For example, "123" == 123 will be coerced to true, because the string "123" is
implicitly converted to the number 123 before the comparison.
When a variable is passed by value, a copy of the variable's value is made and
this value is passed to the function. It means that any changes made to the
variable within the function will not affect the original variable outside of the
function.
function increment(num) {
num += 1;
}
let x = 5;
increment(x);
console.log(x);
// Output: 5
console.log(myArray);
// Output: [1, 2, 3, 4]
(function() {
// Code to be executed here
})();
Using Strict mode for the entire script: To invoke strict mode for an entire script,
put the exact statement "use strict"; (or 'use strict';) before any other
statements.
'use strict';
let strickCode = 'strict mode script!';
v) Octal Literal Syntax: Octal literals (e.g., 0123) are not allowed in strict mode.
vi) With Statement: The use of the with statement is not allowed in strict mode.
The with statement can make code less predictable and hard to optimize.
viii) This Binding in Functions: In strict mode, the this value is undefined in
functions that are not methods or constructors.
Higher Order Functions (HOF) are functions which takes one or more functions
as arguments or return a function as a result.
In JavaScript, functions are first-class citizens, which means that they can be
assigned to variables, passed as arguments, and returned from functions.
2). Filter: The filter function is used to create a new array with all elements that
pass the test implemented by the provided function.
// Output: [2, 4]
3). Reduce: The reduce function is used to reduce an array to a single value by
applying a function to each element of the array.
//Output: 15
function higherOrderFunction(func) {
console.log("Before calling the argument function");
func();
console.log("After calling the argument function");
function sayHello() {
console.log("Hello!");
}
higherOrderFunction(sayHello);
// Output
Before calling the argument function
Hello!
After calling the argument function
function greet() {
return function() {
console.log("Hello World!");
};
}