This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function greet(message, username) { | |
| // undefined are FALSY values in JS | |
| // !undefined means !false -> true | |
| if(!username) { // if username is not yet defined | |
| username = "user"; // we are assinging it some value | |
| } | |
| var greetMessage = `Hello, ${username}! ${message}`; | |
| console.log(greetMessage); | |
| } | |
| greet("Good evening"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var totalSum = 0; // this is variable with global scope | |
| function sumTwoNumbers(x, y) { | |
| if(!y) { // this checks if y is undefined | |
| // if y is undefined, it will not | |
| // enter if block | |
| totalSum = totalSum + x; | |
| }else { | |
| totalSum = x + y; | |
| // here total sum becomes 300 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var x = undefined; | |
| console.log(x); // undefined | |
| console.log(!x); // undefined in JS is a FALSY value | |
| // !false means true | |
| if(!x) { // since this is true, it will execute if block | |
| console.log("inside if"); | |
| }else { | |
| console.log("inside else"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function sum(x, y) { | |
| var z = x + y; | |
| return z; // return back the value of | |
| // z | |
| } | |
| var zz = sum(5 , 6); | |
| // sum(5 , 6) evaluates to 11 | |
| // sum(10, 20) evaluates to 30 | |
| console.log(zz); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var x = 7; | |
| console.log(x); // 7 | |
| console.log(y); // undefined | |
| var y; // Because of hoisting, the variable y | |
| // will be PULLED upwards just before the program | |
| // starts its execution |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| sum(); // sum func will be pulled up b.cz of hoisting | |
| mult(); // mult func will be pulled up b.cz of hoisting | |
| div(); // the division function will be pulled up because of | |
| // hoisting | |
| function sum() { | |
| console.log("sum"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var zz = function(x, y) { | |
| return x + y; | |
| } // Arjun created this general definition (anonymous) for other | |
| // developers | |
| var addTwoNumbers = zz; // Aditi says, that I will start | |
| // using zz to add two numbers, but before I will give it | |
| // a proper name | |
| var result = addTwoNumbers(1, 2); | |
| console.log(result); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var xx = () => { | |
| return "hey there!!" | |
| } | |
| var f = xx(); | |
| console.log(f); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function multiply(a, b) { | |
| return a * b; | |
| } | |
| function square(n) { | |
| return multiply(n, n); | |
| } | |
| function printSquare(n) { | |
| let result = square(n); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function greet() { | |
| console.log("Good evening"); // print the message | |
| greet(); // call greet function | |
| } | |
| greet(); |