Interview Questions- Part-13

JSON.PARSE USAGE IN LOCAL STORAGE

				
					let employee = {
  name: "Anurag",
  code: "empCode-123",
};

const obj = JSON.stringify(employee);
console.log(obj); //{"name":"Anurag","code":"empCode-123"} -->in string

localStorage.setItem("key", obj);

//WRONG KEY
console.log(JSON.parse(localStorage.getItem("key1"))); //null
//CORRECT KEY 
console.log(JSON.parse(localStorage.getItem("key"))); //{name: 'Anurag', code: 'empCode-123'}
//CORRECT KEY WITOUT PARSE
console.log(localStorage.getItem("key")); //{name: 'Anurag', code: 'empCode-123'} -->in string...
				
			

Nested destructuring

				
					let employee = {
  name: "Anurag Nayak",
  fullName: {
    firstName: "Anurag",
    lastName: "Nayak",
  },
  code: "empCode-123",
};

let { fullName } = employee;
console.log(fullName);

let {
  fullName: { lastName },
} = employee;
console.log(lastName);


//this gives you undefined as lastName1 doesnt exist
let {
  fullName: { lastName1 },
} = employee;
console.log(lastName1);
				
			

Rest parameters cannot be at middle of the arguments...

				
					//ERROR
const classAttendance=(students,...teachers,principal)=>{
  console.log(students);
  console.log(teachers);
  console.log(principal);
}
classAttendance(["Student1", "Student2", "Student3"], ["teacher1", "teacher2"], "principal");
				
			
				
					//NO ERROR
const classAttendance = (principal, teachers, ...students) => {
  console.log(principal); //principal
  console.log(teachers); //['teacher1', 'teacher2']
  console.log(students); //['Student1', 'Student2', 'Student3']
};

classAttendance("principal", ["teacher1", "teacher2"], "Student1", "Student2", "Student3");

				
			

Object referencing - Guess the output

				
					let emp = { name: "Anurag" };
let copyemp = [emp];
console.log(copyemp); //below output
//output
/*
[
  {
      "name": "Anurag"
  }
]
*/
console.log(Array.isArray(copyemp)); //true
emp = null;
console.log(copyemp); //below output
//output
/*
[
  {
      "name": "Anurag"
  }
]
*/
console.log(emp); //null
				
			

Object referencing - Guess the output

				
					// Now change the property of original emp
let emp = { name: "Anurag" };
let copyemp = [emp];
emp.name = null;
console.log(copyemp);
/*
[
  {
      "name": null
  }
]
*/
				
			

Guess the output

				
					const input = { number: 5 };
const Addition = (x = { ...input }) => {
  console.log((x.number += 10));
};
Addition(); //15 -->clone is done using spread operator..doesnt affect original input
Addition(); //15 -->clone is done using spread operator..doesnt affect original input
Addition(); //15 -->clone is done using spread operator..doesnt affect original input
Addition(input); //15(5+10)...it affects the origical input
Addition(input); //25(15+10)..it affects the origical input
Addition(input); //35(25+10)..it affects the origical input

				
			

Leave a Comment