javascript ans
javascript ans
1. What is JavaScript?
a. JavaScript is a high-level, interpreted programming language primarily used
for client-side web development. It can also be used server-side (e.g.,
Node.js).
2. What are the data types in JavaScript?
a. JavaScript has 7 primitive data types: undefined, null, boolean, number,
string, symbol, and bigint. It also has object as a non-primitive data type.
3. What is the difference between var, let, and const?
a. var: Function-scoped and can be re-declared and updated.
b. let: Block-scoped and can be updated but not re-declared.
c. const: Block-scoped and cannot be updated or re-declared.
4. What is the use of strict mode in JavaScript?
a. strict mode helps catch common coding mistakes and prevents the use of
potentially problematic features (e.g., assignments to undeclared variables).
5. Explain type coercion in JavaScript.
a. Type coercion is the automatic or implicit conversion of values from one type
to another (e.g., adding a number to a string results in string concatenation).
6. What are undefined and null?
a. undefined: A variable that has been declared but not assigned a value.
b. null: A value representing the intentional absence of any object value.
7. What is a closure in JavaScript?
a. A closure is a function that has access to its own scope, the outer function’s
scope, and the global scope.
8. What is hoisting in JavaScript?
a. Hoisting refers to JavaScript’s behavior of moving variable and function
declarations to the top of their containing scope during compile phase.
9. Explain the difference between == and ===.
a. == (loose equality) compares values after type conversion.
b. === (strict equality) compares values and types without type conversion.
10. What is the purpose of the this keyword?
• this refers to the current context or object on which a method is being invoked.
Asynchronous JavaScript
61. What is the difference between a for...in and a for...of loop in JavaScript?
• for...in is used to loop through the keys of an object, while for...of is used to
loop through iterable objects like arrays, strings, etc.
62. What is the difference between Object.create() and new in JavaScript?
• Object.create() creates a new object with the specified prototype object and
properties, while new creates an instance of a function and sets its prototype to the
function’s prototype.
63. What are the different ways to define a function in JavaScript?
• Functions can be defined as function declarations, function expressions, and arrow
functions.
64. What is the purpose of setImmediate() in JavaScript?
• setImmediate() is used to execute a function after the current event loop cycle,
and is primarily used in Node.js.
65. What is the difference between call(), apply(), and bind()?
• call() and apply() invoke a function immediately with a specified this context,
while bind() creates a new function that, when invoked, has a specified this
context.
66. What is the difference between Object.keys() and Object.values()?
• Object.keys() returns an array of an object's property names, while
Object.values() returns an array of the object's property values.
67. How would you clone an object in JavaScript?
• You can clone an object using Object.assign(), the spread operator
({...object}), or deep cloning techniques like
JSON.parse(JSON.stringify(object)).
68. What is the typeof operator in JavaScript?
• typeof is used to determine the type of a variable or expression. It returns a string
representing the type of the operand.
69. What are the differences between null, undefined, and NaN in JavaScript?
• null is an intentionally assigned empty value, undefined represents a variable that
has been declared but not assigned a value, and NaN represents a computation that
cannot result in a valid number.
70. What is the in operator in JavaScript?
• The in operator checks whether a property exists in an object or its prototype chain.