Javascript Coercion and Tricky Interview questions by Gourav

preview_player
Показать описание
In this video we will uncover a lot of different tricky JS interview questions. Most of them are related to coercion (implicit type casting). And we will have fun while doing it.

Here is the quick definition of Coercion in JS:
Type coercion is the process of converting value from one type to another (such as string to number, object to boolean, and so on). Any type, be it primitive or an object, is a valid subject for type coercion.
Рекомендации по теме
Комментарии
Автор

Excellent and awesome way to deliver the concepts. Amazing..

shafiq.hussain.cscore
Автор

console.log(1+1); // Output -> 2, Data type -> Number




/*When any string or non-string value is added to a
string, it always converts the non-string value
to a string implicitly*/

console.log("1" + "1"); // Output -> 11, Data type -> String
console.log(1+"1"); // Output -> 11, Data type -> String

const obj= {a:"foo"};
console.log(obj + "1"); // Output -> [object object]1, Data type-> String
console.log([1] + ""); //Output -> 1, Data type-> String





/* Arrays are taken as string */
console.log([1] + [1]) ; //Output -> 11, Data type-> String
console.log([3, 1] + [1, 2]); // Output -> 3, 11, 2, Data type -> String


//String - String = Not an Number
console.log([3, 1] - [1, 2]); // Output -> NaN





/*When an operation like subtraction (-), multiplication (*),
division (/) or modulus (%) is performed,
all the values that are not number are converted into the number*/
console.log([1] - [1]); // Output -> 0, Data type -> Number




// + operator with string converts to number
console.log(1 + + "1"); // Output -> 2, Data type -> Number



/* + operator with string => Number but the first operator String so
second operator gets converted back to string*/
console.log("1" + + "1"); // Output -> 11, Data type -> String

kamleshmahapatra