Question #1

const x = [1,2,3];
x[-1] = -1;
console.log(x[x.indexOf(1000)]); // -1

Question #2
Sorting in JS consider element as string

const a = [1,2,15,30,5,45,7];
a.sort()
console.log(a); // [1,15,2,30,45,5,7]

Solution is:

const a = [1,2,15,30,5,45,7];
a.sort((a,b)=> a > b)
console.log(a); // [1,2,5,7,15,30,45]

Question #3

// let i = ?
console.log(i * i); // 0
console.log(i + 1); // 1
console.log(i - 1); // -1
console.log(i / i); // 1

Solution is:

let i = Number.MIN_VALUE;

MIN_VALUE is the smallest number in JS!

let i = Number.MAX_VALUE;
console.log(i * i); // 1.7976931348623157e+308
console.log(i + 1); // Infinity
console.log(i - 1); // 1.7976931348623157e+308
console.log(i / i); // 1

Question #4
+operates on strings & number, so if it’s not a string or number it would try to convert into them…

let x = [1,2,3] + [4,5,6];
console.log(x) // 1,2,34,5,6

Solutions:
let y = [...[1,2,3], ...[4,5,6]];
console.log(y) // [1, 2, 3, 4, 5, 6]

let z = String([...[1,2,3], ...[4,5,6]]);
console.log(z) // 1, 2, 3, 4, 5, 6

Question #5
In JS there’s a MAX_SAFE_INTEGER, if you go beyond that JS put the zeros after

// 18 cifre!
console.log(555555555555555555) // 555555555555555600
console.log(Number.MAX_SAFE_INTEGER) // 9007199254740991

Question #6
The first block is called IIFE (immediately invoked function expression) and creates a block scope and a is defined as block scope variable.
b is not defined with let and it becomes global!

(function(){
   let a = b = 100;
})();
console.log(a);  // is not defined
console.log(b);  // b=100

Question #7
NaN compared to anything is always false, even comparing to itself!

console.log(NaN === NaN);  // false

Question #8
Explain event delegation:

JS event listeners fire not only on a single DOM element but on all its descendants

Question #9
Describe event bubbling.

It’s the inverse of event delegation. Also know as propagation, events on an element will “bubble up” and also fire on all parents

Question #10
Q: Explain hoisting
A: All variables (var) are declared at the top of any given function scope whether you like it or not (includes function declarations)

Hoisting example: https://levelup.gitconnected.com/node-js-best-practices-using-modern-features-1f5a4a189ec8

Question #11
What’s the difference between a variable that is:
– null
– undefined
– undeclared

Undeclared
Trying using a variable that has not been defined
Undefined
Declared but but no defined value (not initialized)
Null
– has a value, its value is null
– null is a “nothing” value
– not zero, not an empty string/object/array
– falsy

….


0 commenti

Lascia un commento

Segnaposto per l'avatar