You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: JavaScript-Quick-Reference.md
+30-1
Original file line number
Diff line number
Diff line change
@@ -21,6 +21,7 @@ A comprehensive yet concise quick-reference and overview of JavaScript fundament
21
21
-[2.2 Basic Data Types](#22-basic-data-types)
22
22
-[2.3 Syntax and Examples](#23-syntax-and-examples)
23
23
-[2.4 Operators](#24-operators)
24
+
-[2.5 Type Conversion and Coercion](#25-type-conversion-and-coercion)
24
25
25
26
## Fundamental Programming Concepts
26
27
@@ -275,7 +276,35 @@ Operators allow you to perform various tasks, such as assigning values to variab
275
276
-`-` Subtracts the second operand from the first operand.
276
277
-`*` Multiplies two operands.
277
278
-`/` Divides the first operand by the second operand.
278
-
-`%` Returns the remainder when the first operand is divided by the second operand.
279
+
-`%` Returns the remainder when the first operand is divided by the second operand
280
+
281
+
## [🔝 Back to Top](#top)
282
+
283
+
## 2.5 TYPE CONVERSION AND COERCION
284
+
285
+
In JavaScript, the conversion and interpretation of values between different types are critical concepts that enable the effective writing and understanding of code. This section focuses on explicit type conversion, also known as type casting, and implicit type coercion, providing insights into their mechanisms and use cases.
286
+
287
+
### Explicit Type Conversion
288
+
289
+
Explicit type conversion, or type casting, involves converting values from one type to another using built-in functions, allowing for controlled and intentional data type changes.
290
+
291
+
-**String Conversion**: To convert a value to a string, use the `String()` function. For example, `String(123)` converts the number `123` into the string `"123"`.
292
+
293
+
-**Number Conversion**: The `Number()` function is used to convert strings and other types to numbers. For instance, `Number("123")` turns the string `"123"` into the number `123`. Inputs that cannot be converted to a number yield `NaN` (Not-a-Number).
294
+
295
+
-**Boolean Conversion**: Using `Boolean()` converts values to booleans. Truthy values (values that are not `0`, `null`, `undefined`, `NaN`, `''`, or `false`) convert to `true`, while falsy values convert to `false`.
296
+
297
+
### Implicit Type Coercion
298
+
299
+
Implicit type coercion happens when JavaScript automatically converts types behind the scenes in the context of an operation.
300
+
301
+
-**Numeric String and Number**: When a numeric string and a number are used in an operation, the string is coerced into a number. For example, `"5" - 2` results in `3`.
302
+
303
+
-**Boolean and Number**: In arithmetic operations, `true` is treated as `1` and `false` as `0`. Thus, `true + 2` equals `3`.
304
+
305
+
-**String Concatenation**: When a number is added to a string, the number is converted into a string. For instance, `5 + "3"` yields `"53"`.
306
+
307
+
Understanding these conversion and coercion rules is essential for debugging and for writing clear, predictable JavaScript code.
0 commit comments