Professional Documents
Culture Documents
Contents
1. What the difference between var, let and const ?...........................................................................1
2. How to get the title of HTML page with JavaScript?........................................................................2
3. What the difference between switch and else if condition in Javascript ?.......................................2
4. What is arrow functions in js ?.........................................................................................................2
5. What the purpose of queryselector in js ?.......................................................................................2
6. Simple way to write if else logic in js using ternary operator ?........................................................2
7. What is Null coalescing(??) ?...........................................................................................................2
8. What is the difference between ‘== ‘ and ‘===’ in JavaScript?........................................................3
9. How to handle errors in Js ?.............................................................................................................3
10. What are Promises in JavaScript? How do they work?................................................................3
11. What is the purpose of the map() function in JavaScript?...........................................................4
12. How does setTimeout() function work in JavaScript?..................................................................4
13. How does the localStorage object work in JavaScript? How is it different from sessionStorage? 4
14. How to set item and get item from local storage ?......................................................................4
15. What is the purpose of the this keyword in JavaScript? How does its value get determined?....5
16. what is closest in js ?....................................................................................................................5
17. How do u convert string to int or float in js ?...............................................................................5
18. How to u check if an element present in array ?..........................................................................5
19. How to use JSON.stringify and JSON.parse in JS ?........................................................................6
20. What the difference between replace and replaceAll in JS ?.......................................................6
21. How to add element in array at first position ?............................................................................6
22. How do you convert a number to a string in JavaScript?.............................................................6
23. How do you get hostname of a website ?....................................................................................6
24. What is optional chaining (?.) ?....................................................................................................6
25. What is a Regular Expression.......................................................................................................6
1. What the difference between var, let and const ?
var has function scope, let has block scope, and const is used for variables that are not intended to be
reassigned.
Var ===function scope + can be re assigned and redeclare +gives you undefined when u access
before declaration
let ==block scope +can be reassigned but cant be redeclare +gives reference error
the switch statement has been known to have better performance in cases where there are a larger
number of cases to check. This is because the switch statement typically uses direct value comparison,
which can be optimized by JavaScript engines for efficient execution.
Cause switch uses jump table (a data structure) and stores addresses of code.
Easy way to write js function we use arrow function using fat arrow operator
Const head1=document .querySelctor(“h1”);gives very first elemnt which matches this selctor
6. Simple way to write if else logic in js using ternary operator ?
The ternary operator provides a concise way to write an if...else statement with a single expression.
If the condition is true, expression1 is evaluated and returned; otherwise, expression2 is evaluated and
returned.
The == operator compares the values for equality after performing type coercion if necessary.
1 == '1' would evaluate to true because the string '1' is coerced into the number 1 before comparison.
The === operator compares the values for equality without performing any type coercion.
1 === '1' would evaluate to false because the number 1 and the string '1' are of different types.
The try...catch statement is used to catch and handle exceptions that occur within a specific
block of code.
The try block contains the code that might generate an error.
If an error occurs within the try block, the catch block is executed, allowing you to handle the
error.
try {
// Code that might throw an error
} catch (error) {
// Handle the error
}
10. What are Promises in JavaScript? How do they work?pending resolved rejected
Promises in JavaScript are used to handle asynchronous operations by representing the eventual
completion or failure of an operation, allowing for more organized and manageable asynchronous code
-Example:
13. How does the localStorage object work in JavaScript? How is it different from
sessionStorage?
localStorage allows data to be stored persistently in the browser, while sessionStorage stores data for the
duration of a session or browsing context.
14. How to set item and get item from local storage ?
To set item
localStorage.setItem("key", "value");
to get item
15. What is the purpose of the this keyword in JavaScript? How does its value get
determined?
The `this` keyword in JavaScript is used to refer to the current execution context or the object on which a
method is invoked, and its value is determined dynamically at runtime based on the function invocation.
Using includes:
if (array.includes(element)) {
console.log("Element is present in the array.");
} else {
console.log("Element is not present in the array.");
}
using indexOf :
While dealing with nested object or function which may be null or defined optional chaining bcomes
Handy it return undefined if value id null or undfined otherwise without optional chianin it will show
error handy
Example
console.log(user.address) // throws error because null does not have address object
console.log( user?.address ) // undefined because user is null so it will break at user and returns
undefined;