Javascript Interview
Javascript Interview
Answer:
A JavaScript function is a block of code designed to perform a particular task. It
is executed when "something" invokes it (calls it).
उत्तर:
एक JavaScript function कोड का एक ब्लॉक होता है जिसे किसी विशेष कार्य को पूरा करने के लिए डिज़ाइन किया गया है। इसे तब
निष्पादित किया जाता है जब "कु छ" इसे कॉल करता है।
How do you define a function in JavaScript?
Answer:
javascript
Copy code
function functionName(parameters) {
// code to be executed
}
JavaScript में function को कै से परिभाषित करते हैं?
उत्तर:
javascript
Copy code
function functionName(parameters) {
// निष्पादित होने वाला कोड
}
What is the purpose of the return statement in a function?
Answer:
The return statement stops the execution of a function and returns a value to the
caller.
उत्तर:
return कथन एक function के निष्पादन को रोकता है और कॉल करने वाले को एक मान लौटाता है।
Intermediate Level
What are function expressions in JavaScript?
Answer:
A function expression is created when a function is assigned to a variable. The
function can be named or anonymous.
javascript
Copy code
const myFunction = function() {
// code
};
JavaScript में function expressions क्या होते हैं?
उत्तर:
एक function expression तब बनता है जब एक function को एक variable को सौंपा जाता है। function नामित या
गुमनाम हो सकता है।
javascript
Copy code
const myFunction = function() {
// कोड
};
What are arrow functions and how are they different from regular functions?
Answer:
Arrow functions are a shorter syntax for writing functions in JavaScript. They do
not have their own this context, making them useful for non-method functions.
javascript
Copy code
const add = (a, b) => a + b;
Arrow functions क्या होते हैं और ये साधारण functions से कै से भिन्न होते हैं?
उत्तर:
Arrow functions JavaScript में functions लिखने के लिए एक छोटा syntax है। इनके पास अपना this
context नहीं होता, जो इन्हें non-method functions के लिए उपयोगी बनाता है।
javascript
Copy code
const add = (a, b) => a + b;
Explain higher-order functions in JavaScript.
Answer:
Higher-order functions are functions that can take other functions as arguments or
return them as output.
javascript
Copy code
function higherOrder(fn) {
return function() {
fn();
};
}
JavaScript में higher-order functions समझाइए।
उत्तर:
Higher-order functions वे functions होते हैं जो अन्य functions को arguments के रूप में ले सकते हैं या
उन्हें output के रूप में लौटा सकते हैं।
javascript
Copy code
function higherOrder(fn) {
return function() {
fn();
};
}
Advanced Level
What is function currying? Provide an example.
Answer:
Function currying is a technique of transforming a function that takes multiple
arguments into a sequence of functions that each take a single argument.
javascript
Copy code
function curry(fn) {
return function(a) {
return function(b) {
return fn(a, b);
};
};
}
const add = (a, b) => a + b;
const curriedAdd = curry(add);
console.log(curriedAdd(1)(2)); // 3
Function currying क्या है? एक उदाहरण दें।
उत्तर:
Function currying एक तकनीक है जो एक function को जो कई arguments लेता है, उसे functions के अनुक्रम में
बदल देता है जो प्रत्येक एकल argument लेता है।
javascript
Copy code
function curry(fn) {
return function(a) {
return function(b) {
return fn(a, b);
};
};
}
const add = (a, b) => a + b;
const curriedAdd = curry(add);
console.log(curriedAdd(1)(2)); // 3
What is a closure in JavaScript?
Answer:
A closure is a function that has access to its own scope, the scope of the outer
function, and the global scope.
javascript
Copy code
function outerFunction() {
let outerVariable = 'I am from outer function';
return function innerFunction() {
console.log(outerVariable);
};
}
const closureFunction = outerFunction();
closureFunction(); // I am from outer function
JavaScript में closure क्या है?
उत्तर:
Closure एक function होता है जिसे अपना scope, बाहरी function का scope, और global scope तक पहुंच
होती है।
javascript
Copy code
function outerFunction() {
let outerVariable = 'I am from outer function';
return function innerFunction() {
console.log(outerVariable);
};
}
const closureFunction = outerFunction();
closureFunction(); // I am from outer function
What are IIFEs (Immediately Invoked Function Expressions)?
Answer:
An IIFE is a function that runs as soon as it is defined.
javascript
Copy code
(function() {
console.log('IIFE executed');
})();
IIFEs (Immediately Invoked Function Expressions) क्या हैं?
उत्तर:
एक IIFE एक function होता है जो जैसे ही परिभाषित किया जाता है, चल जाता है।
javascript
Copy code
(function() {
console.log('IIFE executed');
})();
Explain the concept of memoization in functions.
Answer:
Memoization is an optimization technique where a function stores the results of
expensive function calls and returns the cached result when the same inputs occur
again.
javascript
Copy code
function memoize(fn) {
const cache = {};
return function(...args) {
const key = JSON.stringify(args);
if (cache[key]) {
return cache[key];
} else {
const result = fn(...args);
cache[key] = result;
return result;
}
};
}
const add = (a, b) => a + b;
const memoizedAdd = memoize(add);
console.log(memoizedAdd(1, 2)); // 3 (calculated)
console.log(memoizedAdd(1, 2)); // 3 (cached)
Functions में memoization की अवधारणा समझाइए।
उत्तर:
Memoization एक optimization तकनीक है जहाँ एक function महंगे function कॉल के परिणामों को संग्रहीत करता है
और जब वही inputs फिर से होते हैं तो cached परिणाम लौटाता है।
javascript
Copy code
function memoize(fn) {
const cache = {};
return function(...args) {
const key = JSON.stringify(args);
if (cache[key]) {
return cache[key];
} else {
const result = fn(...args);
cache[key] = result;
return result;
}
};
}
const add = (a, b) => a + b;
const memoizedAdd = memoize(add);
console.log(memoizedAdd(1, 2)); // 3 (calculated)
console.log(memoizedAdd(1, 2)); // 3 (cached)
These questions cover a range of topics within JavaScript functions, from basic
definitions and syntax to more advanced concepts like closures, currying, and
memoization.
Basics
What is JavaScript?
Answer: Primitive data types include string, number, boolean, null, undefined, and
symbol. Objects are also a data type.
Explain the difference between null and undefined.
Answer: Variables are declared using var, let, or const followed by the variable
name.
What is the difference between var, let, and const?
Answer: var has function scope, let has block scope, and const is used for
constants whose values cannot be changed once defined.
What are JavaScript functions?
Answer: Functions are blocks of code designed to perform a particular task and are
executed when called.
How do you define a function in JavaScript?
Answer: Functions can be defined using the function keyword or as arrow functions
(=> syntax).
What is a callback function?
Answer: Anonymous functions are functions without a name and are typically defined
inline.
Explain hoisting in JavaScript.
Answer: this refers to the object that owns the currently executing code.
What are Immediately Invoked Function Expressions (IIFE)?
Answer: IIFE are functions that are executed immediately after they are created.
How do you handle errors in JavaScript?
Answer: Errors are handled using try, catch, and finally blocks.
What is event delegation in JavaScript?
Answer: Event delegation refers to the technique of using a single event listener
to manage events for multiple elements.
What is the difference between == and === operators?
Answer: == checks for equality after type coercion, while === checks for equality
without type coercion.
What are JavaScript promises?
Answer: Arrow functions are a concise syntax for writing functions in JavaScript,
and they do not have their own this context.
Explain the concept of let and const in block-scoped variables.
Answer: let and const are block-scoped variables introduced in ES6. let allows
reassignment, while const is for constants.
What is destructuring assignment in JavaScript?
Answer: Generator functions allow you to pause and resume function execution,
yielding multiple values over time.
Explain the concept of currying in JavaScript functions.
Answer: These methods are used to manipulate the this context of functions. bind()
creates a new function with a specified this value, while call() and apply() invoke
the function immediately.
What is memoization? How is it implemented in JavaScript?
Answer: The event loop is responsible for executing the code, collecting and
processing events, and executing queued sub-tasks.
How does JavaScript handle hoisting of variables and functions?
Answer: Variable declarations are hoisted to the top of their scope, while function
declarations are fully hoisted.
Explain the concept of prototypal inheritance in JavaScript.
Answer: Examples include Singleton, Factory, Observer, and Module patterns, used to
solve recurring problems in software design.
How does JavaScript handle memory management?
Answer: Synchronous code executes line by line, blocking further execution until it
completes. Asynchronous code allows multiple operations to be performed
concurrently, using callbacks or promises for handling results.
Explain the concept of lexical scoping in JavaScript.
Answer: Immutability can be achieved using Object.freeze() for objects and const
for variables to prevent changes after creation.
What are some performance considerations when writing JavaScript code?
Answer: Minimizing DOM manipulation, optimizing loops and algorithms, using event
delegation, and reducing network requests are key considerations.
40
Basics
JavaScript kya hai?
Answer (English): Primitive data types include string, number, boolean, null,
undefined, and symbol. Objects are also a data type.
उत्तर (Hindi): Primitive data types mein string, number, boolean, null, undefined,
aur symbol shamil hote hain. Objects bhi ek data type hain.
null aur undefined mein antar kya hai?
Answer (English): null represents no value, while undefined means a variable has
been declared but not assigned a value.
उत्तर (Hindi): null kisi value ko darshata hai nahi, jabki undefined ka matlab hota
hai ki variable declare kiya gaya hai par usme koi value assign nahi ki gayi hai.
JavaScript mein variable kaise declare karte hain?
Answer (English): Variables are declared using var, let, or const followed by the
variable name.
उत्तर (Hindi): Variables var, let, ya const ka istemal karke declare kiye jate hain,
jisme variable name ke baad use hota hai.
var, let, aur const mein kya antar hai?
Answer (English): var has function scope, let has block scope, and const is used
for constants whose values cannot be changed once defined.
उत्तर (Hindi): var function scope rakhta hai, let block scope rakhta hai, aur const
un constants ke liye istemal hota hai jinke values ek baar define hone ke baad
badal nahi sakti.
Intermediate
JavaScript mein functions kya hote hain?
Answer (English): Functions can be defined using the function keyword or as arrow
functions (=> syntax).
उत्तर (Hindi): Functions function keyword ka istemal karke ya arrow functions (=>
syntax) se define kiye ja sakte hain.
Callback function kya hoti hai?
Answer (English): Anonymous functions are functions without a name and are
typically defined inline.
उत्तर (Hindi): Anonymous functions naam ke bina functions hote hain jo aam taur par
inline define kiye jate hain.
Advanced
JavaScript mein closures kya hote hain?
Answer (English): Closures are functions that retain access to the variables in
their lexical scope, even when the function is executed outside that scope.
उत्तर (Hindi): Closures functions hote hain jo apne lexical scope mein rahne wale
variables ka access retain karte hain, chahe function us scope ke bahar bhi execute
ho.
this keyword JavaScript mein kya hota hai?
Answer (English): this refers to the object that owns the currently executing code.
उत्तर (Hindi): this us object ko refer karta hai jo abhi execute hone wale code ka
owner hai.
Function currying ka concept samjhaaye.