JavaScript Day 15 — Mastering JavaScript Basics: A Comprehensive Guide for Interview Preparation
100 Days of JavaScript Interview Prep: A Beginner’s Journey


Welcome to Day 15–100 Days of JavaScript Interview Prep: A Beginner’s Journey
Embarking on a 100-day adventure focused on learning, practice, and growth sounds like an incredibly rewarding journey.
DAY 15 (April 6th, 2024) —Function related interview questions in JavaScript.
Question 1— What is Debounce function with example ?
Answer —
A debounce function is a strategy/function commonly used in JavaScript to control the time at which a particular function is invoked.
It ensures that the function is not called more often than a specified time interval, even if the triggering event fires multiple times within that interval.
Usage
Most ecommerce website — we can see the debouncing behavior like typing /searching in input box
function inputWithDebounce(func, delay) {
let timeoutId;
return function(...args) {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
func(...args);
}, delay);
};
}
function processInputWithDebounce(input) {
console.log('Processing Data:', input);
}
// Debounced version of the processInputWithDebounce function with a
const processInput = inputWithDebounce(processInputWithDebounce, 500);
// Simulate rapid key presses (e.g., typing)
processInput('dfdf fdf');Question 2 : Can we consider functions as Objects in JavaScript?
Answer :
Yes, in JavaScript, functions are indeed objects.
Some key points about functions to understand better why we should consider them Objects
- Functions are First-Class Citizens.
- Functions Have Properties and Methods
- Function Constructor
- Prototype
Functions are objects because
- they can have properties
- they can be assigned to variables
- they can have methods
- they can use the function and its properties/methods
// Define a simple function
function myDetails(name) {
console.log('My Name is, ' + name + '!');
}
//Function have properties
myDetails.salary = 0;
// assigned to variables
const sayMyName = myDetails;
// methods
sayMyName.salaryCounter = function() {
this.salary++;
};
// the function and its properties/methods
sayMyName('tommy'); // Output: "Hello, tommy!"
console.log(myDetails.salary); // Output: 0
myDetails.salaryCounter();
console.log(myDetails.salary); // Output: 1In this example, we demonstrate that functions can have properties (salary) and methods (salaryCounter).
We can also assign the function to a variable (sayMyName), demonstrating that functions can be treated like any other object in JavaScript.
Question 3 —What is the Self-Invoking Function and example ?
Answer
A self-invoking function, also known as an Immediately Invoked Function Expression (IIFE), is a function in JavaScript that runs as soon as it is defined.
It does not need to be explicitly called like a regular function; instead, it executes immediately after its creation.
Usage — Self-invoking functions are commonly used in JavaScript and below are some usage /advantages
- Help to creating a private scope for variables,
- Prevent pollution of the global namespace, and
- Help to encapsulate code.
(function() {
console.log("This is a self-invoking function!");
})();In this example, the function is defined and executed immediately
and the message “This is a self-invoking function!” to be logged to the console without the need for an explicit function call.
This syntax can be explained as follows:
- STEP 1 — enclosing the function declaration in parentheses
():
It is necessary to tell the JavaScript engine that what follows is a function expression rather than a function declaration.
Note Tip — A function declaration cannot be immediately followed by ()
(function() {
console.log("This is a self-invoking function!"); // function declaration
})
//Enclosing the function declaration in parentheses2. STEP 2 — Adding another pair of parentheses ():
Note = () immediately invoke the function expression defined in the first step.
It results in the function being executed immediately after its definition.
(function() {
console.log("This is a self-invoking function!"); // function declaration
})() // Adding an additional pair of parentheses
Happy Coding and Stay Tuned :)
Kind is cool !!!
Share knowledge to gain knowledge !!!!
Be kind and treat people with respect !!
If you find my videos helpful, then please subscribe, Claps and follow to support :)
Cheers
Kirti Kaushal





