avatarKirti K

Summary

The provided content outlines JavaScript interview preparation with a focus on Day 15, covering topics such as debounce functions, treating functions as objects, and self-invoking functions (IIFEs).

Abstract

The article "JavaScript Day 15 — Mastering JavaScript Basics: A Comprehensive Guide for Interview Preparation" is part of a 100-day journey aimed at mastering JavaScript fundamentals for interview purposes. It delves into three key JavaScript concepts: the debounce function, which optimizes performance by limiting the frequency of function calls; the object-oriented nature of functions in JavaScript, allowing them to possess properties and methods; and self-invoking functions (IIFEs), which execute immediately upon definition, providing encapsulation and avoiding global namespace pollution. The article provides practical examples and explanations to solidify understanding, catering to beginners embarking on a learning marathon to prepare for technical interviews.

Opinions

  • The author, Kirti Kaushal, emphasizes the importance of understanding JavaScript functions as objects, which is crucial for interviews.
  • The debounce function is presented as a practical tool for improving user experience on e-commerce websites, particularly in search functionality.
  • Self-invoking functions (IIFEs) are highlighted for their ability to create private scope and prevent global namespace pollution, showcasing their utility in JavaScript programming.
  • The article encourages sharing knowledge and kindness within the coding community, suggesting that this approach enhances learning and personal growth.
  • The author invites readers to subscribe and follow for support, indicating a commitment to continuous learning and community engagement.

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 — Kirti Kaushal

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

  1. Functions are First-Class Citizens.
  2. Functions Have Properties and Methods
  3. Function Constructor
  4. Prototype

Functions are objects because

  1. they can have properties
  2. they can be assigned to variables
  3. they can have methods
  4. 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: 1

In 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

  1. Help to creating a private scope for variables,
  2. Prevent pollution of the global namespace, and
  3. 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:

  1. 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 parentheses

2. 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
Thank You -Kirti Kaushal 100 Days of JavaScript Interview Prep: A Beginner’s Journey with Kirti Kaushal

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

Coding
Javascript Tips
Javascript Interview
Beginners Guide
Programming
Recommended from ReadMedium