//* ============================
//* Data Types Section - part 2
//* ============================
//* Concatenation in JavaScript
//? In JavaScript, the + sign is not only used for arithmetic addition but also for string
concatenation. When the + operator is used with strings, it concatenates the strings together.
//? It's important to note that if any operand of the + operator is a string, JavaScript will
treat the other operands as strings as well, resulting in string concatenation. If both operands
are numbers, the + operator performs numeric addition.
// const str = "Hello " + "World";
// [Link](str); output: Hello World
//* Type coercion is the automatic conversion of "values" from one data type to another.
// In JavaScript, type coercion occurs when an operation involves values of different types, and
JavaScript automatically converts one type to another to perform the operation.
//? It is a fundamental part of JavaScript and can be used to make code more readable and
efficient.
//? There are two types of coercion in JavaScript: implicit and explicit. Implicit coercion
happens automatically, while explicit coercion is done manually by the programmer.
//! It's worth noting that type coercion can lead to unexpected results, so it's essential to be
aware of how JavaScript handles these situations.
// let sum = "5" + 10;
// [Link](sum); // output: 510 (string concatenation)
// let num = 5 - "10";
// [Link](num); // output: -5 (string "10" is coerced to a number)
// let num1 = "5" - 10;
// [Link](num1); // output: -5 (string "5" is coerced to a number)
//* ============================
//* Tricky Interview Questions
//* ============================
// [Link](10 + "20"); output: 1020 (string concatenation)
// [Link](9 - "5"); output: 4 (string "5" is coerced to a number)
// [Link]("Java" + "Script"); // output: JavaScript (string concatenation)
// [Link](" " + " "); // [Link](typeof (" " + " ")); // output: string (concatenation of
two strings)
// let sum = " " + 0; output =0
// [Link](sum); // output: " 0" (string concatenation with a space and number 0)
// [Link](0 + " " + 0); // output: " 0 0" (string concatenation with two zeros)
// [Link]("string" + 0); // output: "string0" (string concatenation with a number)
// [Link](typeof sum); // output: string (the result of concatenation is a string)
// [Link]("vinod"-"thapa"); output: NaN (not a number, as both operands are strings and
cannot be subtracted)
// [Link]("vinod" + "thapa"); output: vinodthapa (string concatenation)
// [Link]("vinod" - 5); output: NaN (not a number, as "vinod" cannot be coerced to a number)
// [Link](true + true); output: 2 (both true are coerced to 1 and added)
// [Link](true + false);output: 1 (true is coerced to 1 and false to 0, so 1 + 0 = 1)
// [Link](false + false); output: 0 (both false are coerced to 0 and added)
// [Link](false + true); output: 1 (false is coerced to 0 and true to 1, so 0 + 1 = 1)
// [Link](true - true); // output: 0 (both true are coerced to 1 and subtracted, so 1 - 1 =
0)
// [Link](false - true); output: -1 (false is coerced to 0 and true to 1, so 0 - 1 = -1)