avatarKirti K

Summary

The provided content is a comprehensive guide for interview preparation focused on mastering JavaScript basics, particularly the concepts of pass-by-value, pass-by-reference, coercion, and the differences between typeof and instanceof operators, as part of a 100-day learning journey.

Abstract

The web content outlines Day 10 of a 100-day JavaScript interview preparation journey, delving into advanced topics such as the distinction between primitive and non-primitive data types, how JavaScript handles passing values to functions, and the nuances of type coercion. It provides clear examples and explanations of pass-by-value and pass-by-reference mechanisms, demonstrating how modifications to primitive and non-primitive data types within functions affect the original values. Additionally, the content clarifies the concept of coercion by showing how values can be converted between types, both implicitly and explicitly. The article also contrasts the typeof and instanceof operators, explaining their usage and behavior in determining the type of values or objects in JavaScript. The guide aims to solidify foundational knowledge and prepare readers for technical interviews involving JavaScript.

Opinions

  • The author emphasizes the importance of understanding pass-by-value and pass-by-reference for JavaScript interview preparation.
  • The article suggests that implicit coercion can lead to unexpected results if not properly understood.
  • Explicit coercion is presented as a developer's tool for ensuring values are of the desired type.
  • The author points out a known language quirk where typeof null incorrectly returns 'object'.
  • The use of instanceof is recommended for checking the inheritance chain of an object, which can be crucial in object-oriented JavaScript programming.
  • The guide encourages a culture of sharing knowledge and treating people with respect within the programming community.
  • The author invites readers to engage with the content by subscribing, clapping, and following to show support for the educational material provided.

JavaScript Day 10 — Mastering JavaScript Basics: A Comprehensive Guide for Interview Preparation

100 Days of JavaScript Interview Prep: A Beginner’s Journey

DAY 10 (April 1st, 2024) — Continuing Primitive and Non-primitive Data Types Interview Questions

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
Primitive types are passed by value, meaning a copy of the actual value is passed to the function.

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)
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.

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.

  1. typeOf Operator :

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 typeof operator 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 null returns '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 instanceof operator is used to check if an object is an instance of a particular class or constructor function.
  • It returns true if the object is an instance of the specified object type; otherwise, it returns false.
  • 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

Coding Interviews
JavaScript
Beginner Coding
Javascript Interview
Programming
Recommended from ReadMedium