# JavaScript Fundamentals That Finally Make Sense

## ***Template literals (Template strings)***

***Template literals*** are ***literals delimited*** *(determine the limit or boundary)* with ***back-ticks \`\`*** (`define string here`) characters for declaring ***strings***, allowing for ***multi-line strings***, ***string interpolation*** with ***embedded expressions.***

```javascript
let name = 'Ayan'; 
console.log(`Hello ${name}`); // Hello Ayan
```

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><strong><em>Embedding variables:</em></strong>&nbsp;<em>Template literals</em> like <code>Hello ${name}</code> insert <em>variable values</em> directly into <strong><em>strings</em></strong>, producing output such as <code>hello Ayan</code>.</div>
</div>

***Cleaner concatenation:*** They avoid using `+`, making ***string*** creation more readable and easier to write.

> **Template literals take all the** `number` **values and converts them into** `string`**.**

## `Truthy` ***and*** `Falsy` ***values***

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><strong><em>Those values can be called as </em></strong><code>true</code><strong><em> or </em></strong><code>false</code><strong><em> in </em></strong><code>Boolean</code><strong><em> context.</em></strong></div>
</div>

### ***falsy values***

A ***falsy*** value is a value that is considered `false` when encountered in a `Boolean` context.

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><strong><em>falsy values</em></strong><em> are values that are not exactly </em><code>false</code><em>, but will become </em><code>false</code><em> when we try to convert them into a </em><code>boolean</code><em>.</em></div>
</div>

JavaScript uses ***type coversion*** (explicit conversion) to ***coerce*** (implicit convert) any value to a `Boolean` in contexts that require it, such as `conditional` and `loops`.

```javascript
false, undefined, NaN, null, "", 0, 0n, -0
```

### ***truthy values***

In JavaScript a **truthy** value is a value that is considered **true** when encountered in a **Boolean** context.

```javascript
true, 1, "Ayan"
```

> Except **falsy** all values are **truthy**.

### *Type coercion*

Type ***coercion*** is the ***automatic or implicit conversion*** of values from ***one*** data type to ***another*** (such as `strings` to `numbers`).

```javascript
const value1 = "5";
const value2 = 9;
let sum = value1 + value2;

console.log(sum); // 59
```

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><em>It happens whenever </em><strong><em>an operator</em></strong><em> is dealing with </em><strong><em>two values</em></strong><em> that have </em><strong><em>different type</em></strong><em>, behind the scene JavaScript </em><strong><em>convert one</em></strong><em> of the values to </em><strong><em>match</em></strong><em> the other value.</em></div>
</div>

***Explanation***

JavaScript has ***coerced*** the `9` from a `number` into a `string` and then ***concatenated*** the two values together, resulting in a `string` of `59`.

JavaScript had a choice between a `string` or a `number` and decided to use a `string`.

***Why?*** Because it’s defined in it’s `engine`.

*The compiler could have* ***coerced*** *the* `5` *into a* `number` *and returned a* ***sum*** *of* `14`*, but it did not.*

*To return this result, you'd have to* ***explicitly*** ***convert*** *the* `5` *to a* `number` *using the* [*Number()*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) *method:*

```javascript
console.log(Number("9" + 5); // 14
```

### ***How Type Coercion Works?***

In JavaScript, ***type coercion*** mainly occurs in the ***three*** ways:

***String Coercion***

*It occurs when the* `string` *is combined with the* ***non-string*** *using* `(+)`*. JavaScript converts* `numbers` *and* `booleans` *into* `strings` *before concatenation.*

```javascript
console.log("5" + 2); //52
console.log("5" + true); //5true
```

*   *The number* `2` *is* ***coerced*** *to a* `string` *and then concatenated with the* `string "5"`*, resulting in* `"52"`*.*
    
*   *The* `boolean true` *is* ***coerced*** *into the* `string "true"`*, and the* ***two*** `strings` *are concatenated.*
    

***Number Coercion***

*In the* `number` ***coercion***\*, JavaScript converts the\* `string` *into a* `number` *before operating.*

```javascript
console.log("5" - 2); //3
console.log("5" * 2); //10
console.log("10" / "2"); //5
```

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><em>With </em><code>+</code><em> operator, JavaScript performs </em><strong><em>concatenation</em></strong><em>. Except </em><code>+</code><em> operator, JavaScript performs </em><strong><em>arithmetic operations</em></strong><em>.</em></div>
</div>

***Boolean Coercion***

*JavaScript treats the* `truthy / true` *value as* `1` *and the* `falsy / false` *value as* `0`*.*

```javascript
console.log(Boolean("hello")); //true
console.log(Boolean(0)); //false
console.log(Boolean([])); //true
```

> **Non-empty** `strings` are **coerced** to `true`, while `0` is **coerced** to `false`.

### ***Common Issues of Type Coercion***

***Comparing Different Data Types***

***Comparison Operator*** `(==)`*, allows* ***coercion*** *due to which the* ***unexpected conversions*** *occur. To avoid this, we should use the* ***strict equality*** `(===)` *operator.*

```javascript
console.log(0 == "0"); //true
console.log(0 == false); //true
console.log(" " + 0 == 0); //true
```

***Operations on null and undefined***

`Null` *and* `undefined` *behave unexpectedly.*

```javascript
console.log(null == undefined); //true
console.log(null === undefined); //false
console.log(null + 1); //1
```

`NaN` ***Comparisons***

`NaN` *is not equal to* ***itself***\*, so checking with\* `isNaN()` *is the best way to detect it.*

```javascript
console.log(NaN == NaN); //false
console.log(isNaN(NaN)); //true
```

### ***Best Practices to Avoid Type Coercion Issues***

***Use*** `===` ***Instead of*** `==`

*When we use* ***strict equality*** `(===)`*, instead of the* ***comparison operator / loose equality*** `(==)`*, it prevents unnecessary types of* ***coercion***\*.\*

```javascript
console.log(5 === "5"); //false
```

`===` *ensures no* ***implicit type conversion*** *occurs and both values must be of the* ***same*** *type.*

***Use Explicit Conversion***

***Explicit conversion*** *converts the value* ***manually*** *due to which there are fewer chances of errors in the code.*

```javascript
console.log(Number("123")); //123
```

*This ensures that you're working with the* ***correct type***\*, reducing the chance of errors during operations.\*

***Avoid False Value Confusion***

> Always check for `null`, `undefined`, or `“”` **empty strings** explicitly.

```javascript
if (value !== null && value !== undefined) {
    console.log("Value exists");
}
```

> This ensures that only **non-null** and **defined** values are considered valid.

***Use*** `parseInt()` ***and*** `parseFloat()` ***for*** `Number` ***Conversion***

```javascript
console.log(parseInt("42px")); //42
console.log(parseFloat("3.14abc")); //3.14
```

> This will parse the **number part of a string**, ensuring a **valid** numeric conversion.

***Handle*** `NaN` ***Properly***

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><em>Use </em><code>isNaN()</code><em> to check if a value is </em><code>NaN</code><em> instead of comparing it directly.</em></div>
</div>

```javascript
if (isNaN(value)) {
    console.log("Invalid number");
}
```

> This ensures you're correctly detecting **NaN** and handling it appropriately.

### ***Type conversion***

***Manually*** or ***explicitly*** convert the ***type*** of a value from ***one data-type*** to ***another***.

***Original value doesn’t converted.***

*JavaScript can only convert to* ***three*** *types. we can convert to a* `number`*, to a* `string`*, or to a* `boolean`*.*

```javascript
const inputYear = "1996";
console.log(Number(inputYear), inputYear);
```

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><strong><em>Which JavaScript concept do you find the most confusing? Share it in the comments, and let’s learn together.</em></strong></div>
</div>
