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

Welcome to Day 13–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 13 (April 4th, 2024) — Continuing Functions in JavaScript with examples
Question 1- What is the concept of pure functions with example JavaScript?
Answer :
A pure function is a function that always returns the same result given the same inputs .
It has no side effects and doesn’t modify any external variables or state, and its output only depends on its inputs.
function add(a, b) {
return a + b;
}In this example, the add function takes two parameters, a and b, and returns their sum.
This function is pure because it doesn't modify any external variables or state, and its output only depends on its inputs.
Another example
function square(x) {
return x * x;
}The square function takes a single parameter x and returns its square.
Consider now Impure function to understand it better.
let total = 0;
function addToTotal(x) {
total += x;
return total;
}This function addToTotal modifies the external variable total every time it's called, which means its behavior is dependent on and modifies external state.
This makes it impure because its output not only depends on its input but also on the current state of the total variable.
Advantages :
Easy to test, debug and reason about.
They facilitate functional programming paradigms such as immutability and referential transparency
Question 2 : Give an example when a function called itself?
Similar question — Create a recursive function (factorial)
Answer : Can a function called itself ? Yes, it can
This concept is known as recursion, and functions that call themselves are called recursive functions.
function factorial(n) {
// Base case: if n is 0 or 1, return 1
if (n === 0 || n === 1) {
return 1;
}
// Recursive case: n * factorial(n - 1)
return n * factorial(n - 1);
}
// Calculate factorial of 5
console.log(factorial(5)); // Output: 120Important Tip : Recursive thinking is really important in programming.
Advantages
It helps you break down bit problems into smaller ones.
Disadvantages
It can consume a lot of memory.
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






