Interview Questions- Part-16

Error?

				
					let @anurag = "anurag";
console.log(@anurag);
//VM3491:1 Uncaught SyntaxError: Invalid or unexpected token

				
			

How to delete a property in object

				
					let obj = {
  age: 15,
  name: "Rohan",
};

delete obj.age;
console.log(obj);//{name: 'Rohan'}

				
			

Guess the output

				
					let num = [1, 2, , 4, 5, null];
console.log(num[2]);//undefined
				
			

Slice vs Splice

				
					//*******************************SPLICE*************************************************
//THE INDEX WILL BE INCLUSIVE -->
//[1,2,3,4,5]
//1. SPLICE(1,3) --> INDEX 1 AND 3 WILL BE INCLUSIVE..
//NEW ARRAY --> THE REMOVED OR SPLICED ONE --> INDEX INCLUSIVE --> [2,3,4]
//ORIGINAL ARRAY --> LEFT ONE --> [1,5]

//2. SPLICE(1,3,"X")
//[1,2,3,4,5]
//NEW ARRAY --> THE REMOVED OR SPLICED ONE --> INDEX INCLUSIVE --> [2,3,4]
//ORIGINAL ARRAY --> LEFT ONE BUT WITH UPDATED VALUE X IN BETWEEN THE REMOVED INDEX--> [1,"X",5]

let original = [1, 2, 3, 4, 5];
let newArray = original.splice(1, 3);
console.log(original); //[1,5]
console.log(newArray); //[2,3,4]

let original1 = [1, 2, 3, 4, 5];
let newArray1 = original1.splice(1, 3, "anurag");
console.log(original1); //[1,"anurag",5]
console.log(newArray1); //[2,3,4]

//*******************************SLICE****************************************************
//ORIGINAL ARRAY IS NOT AFFECTED
//NEW ARRAY -->START INDEX IS INCLUSIVE, END INDEX IS EXCLUSIVE --> [2,3]

let original2 = [1, 2, 3, 4, 5];
let newArray2 = original2.slice(1, 3);
console.log(original2); //[1,2,3,4,5]
console.log(newArray2); //[2,3]

				
			

let vs var - Random question

				
					for (let i = 0; i < 9; i++) {}
console.log(i); //Uncaught ReferenceError: i is not defined

for (var i = 0; i < 9; i++) {}
console.log(i); //9

				
			

Random question on spread operator

				
					//This is pretty straightforward --> new array will not impact the original array
let num1 = [1, 2, 3, 4, 5];
let copyNum1 = [...num1];
copyNum1[0] = 99;
console.log("copyNum1", copyNum1); //[99, 2, 3, 4, 5]
console.log("num1", num1); //[1, 2, 3, 4, 5]

//-----------------------------------------------------------------------------------------
//But have a look at below question
//Original array gets affected ??? why ?? Array is copied but underlying objects are still accessed by reference
let products = [
  {
    model: "Royal Enfield Interceptor 650",
    price: "2.89 lakhs",
  },
  {
    model: "Royal Enfield Continental",
    price: "3.09 lakhs",
  },
  {
    model: "Royal Enfield Classic 350",
    price: "1.52 lakhs",
  },
];

let copyProduct = [...products];
copyProduct[0].model = "Royal Enfield Himalayan";
console.log(copyProduct[0].model); //Royal Enfield Himalayan
console.log(products[0].model); //Royal Enfield Himalayan

//--------------------------------------------------------------------------------------
//Here it will not impact the original object ..as we are using spread operator to copy the object 
let product1 = {
  model: "Royal Enfield Interceptor 650",
  price: "2.89 lakhs",
};

let product2 = { ...product1 };
product2.model = "Royal Enfield Himalayan";
console.log(product2.model); //Royal Enfield Himalayan
console.log(product1.model); //Royal Enfield Interceptor 650
				
			

Spread Operator one more usage in Date()

				
					let date1 = new Date(2022, 8, 2022);
console.log(date1); //Tue Mar 14 2028 00:00:00 GMT+0530 (India Standard Time)

let dateFields = [2022, 8, 2022];
let date2 = new Date(...dateFields);
console.log(date2); //Tue Mar 14 2028 00:00:00 GMT+0530 (India Standard Time)
				
			

Guess the output

				
					console.log(Number.isNaN(NaN)); //true
//isNaN
//--> checks if its number or not
//-->does type coercion ?? what does it mean ...check below code
console.log(Number.isNaN("2")); //false
				
			

Guess the output

				
					//error::->ReferenceError: x is not defined
try {
  console.log(x);
} catch (err) {
  console.error("error::->" + err);
}


//********************************************

try {
  console.log(x);
} catch {
  console.error("error");
}
//it gives you the error 
//catch no need to take parameter..its optional now
				
			

Generate Random number

				
					//Generate Random Number
Math.random(); //gives you decimal between 0 and 1

//I want to generate random number between 1 and 100
Math.ceil(Math.random()) * 100;

//I want to generate random number between 1 and 10
Math.ceil(Math.random()) * 10;

//I want to generate random number between 20 and 30
Math.ceil(Math.random()) * 10 + 20;

//I want to generate random number between 20 and 50
Math.ceil(Math.random() * (50 - 20)) + 20;

				
			

Guess the output on IIFE

				
					//IIFE
(function (a, b) {
  console.log((a * b) / b + a);
})(10, 9);

//key points on IIFE
//JS considers above as function expression as it is not starting with function keyword
//func expression means as below
// const x = () =>{} ...
//IIFE is not hoisted by JS engine
				
			

Leave a Comment