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


Welcome to Day 10–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 10 (April 1st, 2024) —Continuing Primitive and Non-primitive Data Types Interview Questions.
Question 1 — Explain the pass-by-value and pass-by-reference concept in JavaScript with respect to primitive and non-primitive data types with examples?
Answer :
- As we discussed in our Day 9 — Primitive types are passed by value, meaning a copy of the actual value is passed to the function.
// Pass by value
let num = 10;
function changeNum(value) {
value = 20;
}
changeNum(num);
console.log(num); // Output: 10
In this example, num is passed to the changeNum function. Inside the function, the parameter value is modified, but it does not affect the original value of num.
- As we discussed in our
Day 9— Non-primitive types are passed by reference, meaning the reference (memory address) of the object is passed to the function. - It means — When a non-primitive value is passed to a function, the reference to the original object is passed, not a copy of the object itself.
// Pass by reference
let obj = { name: "John" };
function changeName(person) {
person.name = "Jane"; // Modifying the object's property
}
changeName(obj);
console.log(obj.name); // Output: Jane
// / Output: "Jane" (original object is modified)
In this example, “obj” object is passed to the changeName function. Inside the function, the property name of the object is modified.
Since objects are passed by reference, the modification affects the original "obj" object.
Conclusion
- Pass-by-Value: Involves making a copy of the actual value, so changes inside the function do not affect the original value.
- Pass-by-Reference: Involves passing the reference to the original object, so changes inside the function affect the original object.
Question 2— How we can explain coercion with examples when in relation with Primitive and non-primitive?
Answer :
As we discussed in our Day1 “coercion” refers to the process of conversion of a value from one data type to another, either implicitly or explicitly.
let myNumOfHouses = 10 // Primitive type
console.log("Number :" + myNumOfHouses)

Conclusion
The number 10 is implicitly coerced into a string when it is concatenated with another string.
Another Example
let myname = "12345"
let myage= Number(myname);
console.log(myage) // Output : 12345 === number
Conclusion :
Here, the string 12345 is explicitly coerced into a number using the Number() function.
Question 3 — Discuss the difference between typeof and instance of operators in JavaScript ?
Answer :
In JavaScript, both typeof and instanceof are operators used to determine the type of value or object, but they serve different purposes and have different behaviors.
typeOfOperator :
In JavaScript, both typeof and instanceof are operators used to determine the type of value or object, but they serve different purposes and have different behaviors.
- The
typeofoperator is used to determine the data type of value or expression. - It returns a string indicating the type of the operand.
- It’s typically used to check primitive data types and function types.
- It’s important to note that
typeof nullreturns'object', which is considered a bug of language
typeof 10; // 'number'
typeof 'hello world'; // 'string'
typeof true; // 'boolean'
typeof undefined; // 'undefined'
typeof null; // 'object'
typeof {}; // 'object'
typeof []; // 'object'
typeof function(){};// 'function'
2) instanceof Operator
- The
instanceofoperator is used to check if an object is an instance of a particular class or constructor function. - It returns
trueif the object is an instance of the specified object type; otherwise, it returnsfalse. - It’s typically used with objects created from user-defined constructor functions or built-in constructor functions like
Array,Date, etc.
var arr = [];
arr instanceof Array; // true
arr instanceof Object // true
arr instanceof Date. // false 
Another Example for understanding the instanceof operator
function Book(name,year){
this.name = name ;
this.year= year;
}
let myBook = new Book('children book',2003)
myBook instanceof Book 
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

Other articles






