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


Welcome to Day 17–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 17 (April 8th, 2024) — Core Functions and ES6 with Functions Interview Questions with example?
Question 1 — What are Arrow Functions in ES6? How do they differ from traditional JavaScript functions?
Answer —
Arrow functions are a concise syntax for writing functions in JavaScript introduced in ES6.
- They have a shorter syntax compared to traditional function expressions.
- They do not have their own
thiscontext; instead, they inheritthisfrom the surrounding lexical context.
// Traditional function expression
const add = function(x, y) {
return x + y;
};
// Arrow function
const add = (x, y) => x + y;
Question 2 — What is the purpose of the const keyword in ES6? How does it relate to functions?
Answer —
The const keyword is used to declare constants in JavaScript.
- When used with functions,
constdeclares a variable whose value cannot be reassigned. - However, if the function is an arrow function or a function expression, the reference to the function itself cannot be reassigned, but the function’s behavior (such as modifying properties) can still change.
const myFunction = () => { /* Function body */ };
// Function reference cannot be reassigned
myFunction = someOtherFunction;
// Error: Assignment to constant variable
const keyword in ES6? How does it relate to functions?Let’s check function behavior — How we can be modifying the properties ?
When using
constwith functions in JavaScript, it's important to understand that while the reference to the function itself cannot be reassigned, the function's behavior, such as modifying properties or internal state, can still change.
const person = { // Define a constant object with a property and a method
name: 'tom',
age: 30,
greet() {
console.log(`Hello, my name is ${this.name} and I'm ${this.age} years old.`);
}
};
// Attempt to reassign the 'person' variable (will result in an error)
// Error: Assignment to constant variable
person.name = 'jerry';// person = {};
// Modify properties of the 'person' object (allowed)
person.age = 10;
// Call the 'greet' method
person.greet(); // Output: "Hello, my name is jerry and I'm 10 years old."
const changeAge = (newAge) => {
// Define a constant function that modifies the 'person' object
person.age = newAge;
};
// Call the 'changeAge' function to modify the 'age' property of the 'person' object
changeAge(35);// Call the 'greet' method again to see the updated age
person.greet(); // Output: "Hello, my name is jery and I'm 35 years old."
const keyword in ES6? How does it relate to functions?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

#CodingInterview #coding #javascript #javascriptInterview #interviews




