[3,4,6,7][1,2,3] = 7? Javascript Tricky Interview question explained by Frontend Master #javascript

preview_player
Показать описание
Javascript Interview Questions

Рекомендации по теме
Комментарии
Автор

Keep going sir . I like the way you teach and do some xss attack in real time

rushichebale
Автор

array[index] // index (means one) but not indices/indexes

let r = [9, 8, 7, 6][1, 2, 3, 0]; // [array][index]
index = 0 1 2 3 // starts with 0 and the increment is 1
console.log(r);

When we use the array [1, 2, 3, 0] as an index to access the elements in another array [9, 8, 7, 6],
JavaScript does not throw a syntax error but it rather evaluates [1, 2, 3, 0] as an array expression.
JS only considers the last element a ‘0’ as the index to access the element in the array [9, 8, 7, 6], and the other elements 1, 2, and 3 are ignored in this context.

Output: 9 // Index considered by JS is only the last element, the right extreme One, in this case 0.

let r = [9, 8, 7, 6][3] // Output 6 as element at index 3 is 6
index = 0 1 2 3

let r = [9, 8, 7, 6][1, 3] // Output 6 as element at index 3 is 6
let r = [9, 8, 7, 6][1, 0, 3] // Output 6 as element at index 3 is 6
let r = [9, 8, 7, 6][1, 0, 2, 3] // Output 6 as element at index 3 is 6
index = 0 1 2 3

If we need more than one element, use slice(start_Index_include, end_Index_exclude)
let r = [9, 8, 7, 6].slice(1, 3);
console.log(r)
Output: (2) [8, 7]

function a() {return "a"}
function b() {return "b"}
function c() {return "c"}

console.log(a(), b(), c());
console.log(c(), a(), b());
console.log(b(), c(), a());

Output:
a b c
c a b
b c a

shivakumarm
Автор

could you please answer what will be output for this
let b = [14, 15, 8, 115][1, 2, 3][1];
console.log(b);
as per your statement the value of b should be 8 but it is undefind why??

alokapsensys
Автор

those who are facing pop-up on Answer. Try inspect element and make VISIBILITY:HIDDEN

shujamigo