Essential JavaScript Interview Questions Day130

Essential JavaScript Interview Questions Day130

·

2 min read

Today #Day130 of #365DaysOfCode, Learning Essential JavaScript Interview Questions.

27. What is the output of the following code, and why?

console.log(1 < 2 < 3); 
console.log(3 > 2 > 1);

The first statement returns true which is as expected.

The second returns false because of how the engine works regarding operator associativity for < and >.

It compares left to right, so 3 > 2 > 1 JavaScript translates to true > 1. true has value 1, so it then compares 1 > 1, which is false.

console.log(1<2); // true
console.log(1 <'2'); // true
console.log(1 <'two'); // false

console.log(true,1); // true 1

console.log(true < 1); // false
console.log(false<1); // true
console.log(true < 2); // true
console.log(false<2); // true


console.log(1<2<3); // true
console.log(3>2>1); // false
console.log(3>2>true); // false
console.log(3>2>false); // true

28. How do you add an element at the beginning of an array? How do you add one at the end?

var myArray = ['a', 'b', 'c', 'd'];
myArray.push('end');
myArray.unshift('start');
console.log(myArray); // ["start", "a", "b", "c", "d", "end"]

With ES6, one can use the spread operator:

myArray = ['start', ...myArray];
myArray = [...myArray, 'end'];

Or, in short:

myArray = ['start', ...myArray, 'end'];

Ans

var arr = [1,2,3,4]
console.log(arr); // (4) [1, 2, 3, 4]

arr.unshift('start')
console.log(arr); // (5) ["start", 1, 2, 3, 4]

arr.push('end')
console.log(arr); // (6) ["start", 1, 2, 3, 4, "end"]

arr.shift()
console.log(arr); // (5) [1, 2, 3, 4, "end"]

arr.pop()
console.log(arr); // (4) [1, 2, 3, 4]

var arr2 = ['start',...arr]
console.log(arr2); // (5) ["start", 1, 2, 3, 4]

arr2 = [...arr2,'end']
console.log(arr2); // (6) ["start", 1, 2, 3, 4, "end"]

let arr3 = ['srart',...arr,'end']
console.log(arr3); // (6) ["srart", 1, 2, 3, 4, "end"]

Conclusion

Learned and practiced Essential JavaScript Interview Questions involving topics like arithmetic operators and arrays.

Code

  1. code

Source: JavaScript Interview Questions [Link]

Author: Dheeraj.y

Connect with me:

Did you find this article valuable?

Support dheerajy blog by becoming a sponsor. Any amount is appreciated!