|
| 1 | +function myAdd(a, b) { |
| 2 | +// var oopsGlobal = 5; // This is local |
| 3 | +oopsGlobal = 5; // This is global |
| 4 | +return a + b; |
| 5 | +} |
| 6 | + |
| 7 | +console.log(myAdd(100, 189.987)) |
| 8 | + |
| 9 | + |
| 10 | +// Global Scope |
| 11 | +var myGlobal = 10; |
| 12 | +function myNewAdd(a, b) { |
| 13 | +if (typeof myGlobal != undefined) { |
| 14 | +output = a + b + myGlobal; |
| 15 | +} |
| 16 | +if (typeof oopsGlobal != undefined) { |
| 17 | +output = a + b + oopsGlobal; |
| 18 | +} |
| 19 | +return output; |
| 20 | + |
| 21 | +} |
| 22 | +console.log(myNewAdd(100, 189.987)) |
| 23 | + |
| 24 | + |
| 25 | +// Local variable take precedence over global variable |
| 26 | +var outerWear = "T-Shirt"; |
| 27 | +function myOutfit() { |
| 28 | +var outerWear = "Sweater"; |
| 29 | +return outerWear; |
| 30 | +} |
| 31 | +console.log(myOutfit()); // Sweater |
| 32 | + |
| 33 | + |
| 34 | +// A program to move the list by 1 element |
| 35 | +function nextItem(list, item) { |
| 36 | + |
| 37 | +list.push(item); |
| 38 | +list.shift(); |
| 39 | +return list; |
| 40 | +} |
| 41 | +list = [2, 3, 4, 5, 6] |
| 42 | +// Expected output = [3, 4, 5, 6, 7] |
| 43 | +console.log("Before - ", JSON.stringify(list)); |
| 44 | +console.log(nextItem(list, 7)); |
| 45 | +console.log("After - ", JSON.stringify(list)); |
| 46 | + |
| 47 | + |
| 48 | +//Working with Boolean |
| 49 | +function welcomeToBoolean() { |
| 50 | +return false; |
| 51 | +} |
| 52 | +if (!welcomeToBoolean()) { |
| 53 | +console.log(welcomeToBoolean()); |
| 54 | +} |
| 55 | + |
| 56 | + |
| 57 | +// Equality and Strict-Equality |
| 58 | +console.log(3 == 3); |
| 59 | +console.log(3 === 3); |
| 60 | +console.log(3 == '3'); |
| 61 | +console.log(3 === '3'); // Strict equality opertaor |
| 62 | + |
| 63 | +val = 75 |
| 64 | +if(val >= 18 && val <=60){ |
| 65 | +console.log("You can drive.") |
| 66 | +} |
| 67 | +else console.log("You can't drive.") |
| 68 | + |
| 69 | +/* |
| 70 | +&& - Logical Anb |
| 71 | +|| - Logical OR |
| 72 | +*/ |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + |
0 commit comments