Interview Questions- Part-12

Guess the output

				
					//Guess the output of arr1 and arr2
let arr1 = [1, 2, 3];
let arr2 = [...arr1];
arr2.push(4);
console.log(arr1); //[1,2,3]
console.log(arr2); //[1,2,3,4]
				
			

Guess the output

				
					var employee = {
  name: "Anurag",
  getEmployeeCode: function () {
    return this.name;
  },
};

let accessEmployeeCode = employee.getEmployeeCode;
console.log(accessEmployeeCode());//undefined
console.log(employee.getEmployeeCode());//Anurag
				
			

Guess the output

				
					var employee = {
  name: "Anurag",
  getEmployeeCode: function () {
    return this.name;
  },
};

let accessEmployeeCode = employee.getEmployeeCode.bind(employee);
console.log(accessEmployeeCode()); //Anurag
console.log(employee.getEmployeeCode()); //Anurag
				
			

Guess the output

				
					console.log(typeof typeof 1);
//typeof 1 --> number
//typeof number --> string
				
			

Guess the output

				
					let x = 1;
const guessOutput = () => {
  let x = 4;
  const inner = () => {
    x++;
    var x = 10;
    console.log(x);
  };
  inner();
};
guessOutput();

//Guess the output
//output should be 10...rest all x are distractions
//Its related to closure
				
			

create a private variable in Javascript

				
					const accessPrivateKey = () => {
  let privateKey = "Private Key";
  return function () {
    return privateKey;
  };
};

let getPrivateKey = accessPrivateKey();
console.log(getPrivateKey()); //Private Key

console.log(accessPrivateKey());
//
// ƒ () {
//   return privateKey;
// }
				
			

Guess the output

				
					let obj4 = {};
obj4.title = "learning object!!";
obj4["title"] = "learning new object !!!";
console.log(obj4.title); // learning new object !!!
console.log(obj4["title"]); // learning new object !!!
				
			

Guess the output

				
					let side = 10;
const square = {
  side: 5,
  area: function () {
    return this.side * this.side;
  },
  perimeter: () => 4 * this.side,
};

//25--> this.radius refers to side in square function
console.log(square.area()); 
//NAN --> here side in arrow function refers to window object
console.log(square.perimeter());
				
			
				
					let side = 10;
const square = {
  side: 5,
  area: function () {
    return this.side * this.side;
  },
  perimeter: () => 4 * side,
};

//25
console.log(square.area());

//40
console.log(square.perimeter());
				
			

Guess the output

				
					const address = { city: "Rayagada", state: "Odisha" };
const employee = { name: "Anurag", gender: "Male", ...address };
console.log(employee); //{name: 'Anurag', gender: 'Male', city: 'Rayagada', state: 'Odisha'}

//{"name":"Anurag","gender":"Male","state":"Odisha"}
//it only strigified --> name , gender , state
console.log(JSON.stringify(employee, ["name", "gender", "state"]));
				
			

Guess the output

				
					let obj = {
  name: "Anurag",
  gender: "Male",
  name: "Abhishek",
};

console.log(obj);
//{name: 'Abhishek', gender: 'Male'}
				
			

square the numbers inside object

				
					//Square the numbers inside obj
let obj = {
  num1: 4,
  different: "different",
  num2: 5,
  num3: 6,
  num4: 9,
};


for (let x in obj) {
  if (typeof obj[x] === "number") {
    obj[x] *= obj[x];
  }
}

console.log(obj);
//{num1: 16, different: 'different', num2: 25, num3: 36, num4: 81}

				
			

Guess the output

				
					const x = {};
const y = {};
const z = {};
const u = { key: "key" };

x[y] = 90;
x[u] = 95;
x[z] = 100;

console.log(x[u]);//100

// it becomes like this when you run it
//so finally the value is 100
// x[[object Object]]: 90
// x[[object Object]]: 95
// x[[object Object]]: 100

//x[u] is nothing but -->x[[object Object]] --> the final value is 100
				
			

Leave a Comment