Question: What are the different data types in JavaScript?
|
Answer: JavaScript supports several data types, including primitive types such as numbers, strings, booleans, null, undefined,
and symbol (added in ECMAScript 6), and reference types such as objects and arrays.
|
Question: Explain the difference between '==' and '===' in JavaScript?
|
Answer: The '==' operator checks for equality after type coercion, meaning it tries to convert operands
to the same type before comparison.
The '===' operator, however, checks for strict equality without type conversion. For example, 1 == '1'
would return true, but 1 === '1' would return false.
|
Question: What is closure in JavaScript?
|
Answer: A closure is a feature in JavaScript where a function retains access to its lexical scope even
after the function has finished executing. This allows the function to maintain access to the variables and parameters of
its outer function, even when the outer function has already completed execution.
|
Question: How do you handle asynchronous operations in JavaScript?
|
Answer: Asynchronous operations in JavaScript can be handled using callbacks, promises, or async/await syntax.
• Callbacks are functions passed as arguments to other functions
• Promises provide a cleaner way to handle asynchronous operations using .then() and .catch() methods
• And async/await offers a more readable and synchronous-like way to write asynchronous code using the async function
and the await keyword.
|
Question: Can you explain the concept of prototypal inheritance in JavaScript?
|
Answer: The prototypal inheritance is a fundamental concept in JavaScript where objects can inherit properties
and methods from other objects. In JavaScript, each object has a private property called [[Prototype]] (or __proto__),
which points to another object.
When a property or method is accessed on an object, JavaScript looks for it in the object itself, and if not found,
it looks up the prototype chain until it finds the property or reaches the end of the chain.
|
Question: What are the different ways to define a function in JavaScript?
|
Answer: Below one may find the different ways for function declaration:
• Function Declaration: function functionName() {}
• Function Expression: var functionName = function() {};
• Arrow Function: const functionName = () => {};
• Method Definition: const object = { methodName() {} };
|
Question: What is the difference between null and undefined in JavaScript?
|
Answer: In JavaScript, null is a primitive value that represents the intentional absence of any object value,
while undefined is a primitive value automatically assigned to variables that have just been declared or to formal arguments
for which there are no actual arguments provided.
In essence, null is explicitly assigned by developers, whereas undefined typically represents
unintentional absence or lack of value assignment.
|