Javascript Control flow

Data Types:

Strings:

  • Used to represent text.
  • Enclosed in single (') or double (") quotes.
                                  
                                    let greeting = "Hello, World!";
let name = 'John';                    
                                  
                                

Numbers:

Represents numeric values, including integers and floating-point numbers.

                                  
                                    let count = 10;
let pi = 3.14;
                                  
                                

Booleans:

Represents true or false values.

                                  
                                    let isTrue = true;
let isFalse = false;
                                  
                                

Type Coercion:

Type coercion refers to the automatic or implicit conversion of one data type to another.

JavaScript will try to make sense of operations even if they involve different data types.

Implicit Coercion:

                                  
                                    let num = 5;
let str = "10";

let result = num + str;
console.log(result); // "510" (string concatenation)              
                                  
                                

In this example, the num variable is implicitly coerced into a string when it's added to the string str.


Explicit Coercion:

                                  
                                    let num = 5;
let str = "10";

// Using parseInt for explicit coercion
let result = num + parseInt(str);
console.log(result); // 15 (numeric addition)
                                  
                                

Here, parseInt is used to explicitly coerce the string str into a number before performing the addition.