Module 9 Participation | Shorthand Javascript

1. Ternary Operator for Conditional Assignment:

            // Long form
            let x;
            if (condition) {
                x = 'trueValue';
            } else {
                x = 'falseValue';
            }

            // Shorthand
            let x = condition ? 'trueValue' : 'falseValue';   
        

2. Nullish Coalescing Operator:

            // Long form
            let myVar = null;
            let result = myVar !== null && myVar !== undefined ? myVar : "default value";

           // Shorthand
           let myVar = null;
           let result = myVar ?? "default value";
        

3. Arrow Functions:

            // Long form
            function add(a, b) {
                return a + b;
            }

            // Shorthand
            const add = (a, b) => a + b;
        

4. Assigning Values to Multiple Variables:

            // Long form
            let x, y, z;
            x = 'big';
            y = 'small';
            z = 'medium';

            // Shorthand
            let [x, y, z] = ['big', 'small', 'medium'];
        

5. Object Property Shorthand:

            // Long form
            const product = 'Fluffy Bunny';
            const quantity = 30;
            const item = {
                product: product,
                quantity: quantity
            };

            // Shorthand
            const item = { product, quantity };
        

6. Destructuring Assignment:

            // Long form
            const user = { name: 'Alice', age: 25 };
            const name = user.name;
            const age = user.age;

            // Shorthand
            const { name, age } = { name: 'Alice', age: 25 };
        

7. Swap Two Variables:

            //Long form
            let a = 5;
            let b = 10;
    
            let temp = a;
            a = b;
            b = temp;

            //Shorthand
            let a = 5;
            let b = 10;

            [a, b] = [b, a];
        

8. Repeat a String Multiple Times:

            //Long form
            let str = "Money ";
            let repeatedStr = "";
            for (let i = 0; i < 10; i++) {
            repeatedStr += str;
            }

            //Shorthand
            'Money '.repeat(10);
        

9. Template Literals:

            // Long form
            const name = 'Bob';
            console.log('Hello, ' + name + '!');

            // Shorthand
            const name = 'Bob';
            console.log(`Hello, ${name}!`);
        

10. AND Short-circuit Evaluation:

            //Long form
            let isLogged = true;
            let isAdmin = false;
            if (isLogged) {
                if (isAdmin) {
                  console.log("User is logged in and is an admin.");
                } else {
                  console.log("User is logged in, but is not an admin.");
                }
              } else {
                console.log("User is not logged in.");
              }

            //Shorthand 
            let isLogged = true;
            let isAdmin = false;
            
            isLogged && isAdmin && console.log("User is logged in and is an admin.");