|
| 1 | + |
| 2 | + |
| 3 | +/* |
| 4 | +1. What is the Comma Operator? |
| 5 | +The comma operator allows multiple expressions to be evaluated in a single statement. |
| 6 | + It evaluates each expression from left to right, but it only returns the value of the last expression. |
| 7 | +
|
| 8 | +1. const x=(4,5,6) |
| 9 | +console.log("x =",x)//6 |
| 10 | +console.log(typeof x)//number |
| 11 | +
|
| 12 | +
|
| 13 | +2. |
| 14 | +function abc() { |
| 15 | +} |
| 16 | +
|
| 17 | +var abc |
| 18 | +
|
| 19 | +console.log(abc);//funtion :abc |
| 20 | +
|
| 21 | +
|
| 22 | +
|
| 23 | +3. console.log(n.toString(8)); |
| 24 | +n.toString(8) converts the number n (which is 10) into its string representation in base 8 (octal). |
| 25 | +
|
| 26 | +In base 8, 10 in decimal is represented as 12 (because 1 * 8^1 + 2 * 8^0 = 10 in decimal). |
| 27 | +
|
| 28 | +Output: 12 |
| 29 | +
|
| 30 | +So, the second log prints "12", which is the base 8 (octal) representation of 10. |
| 31 | +
|
| 32 | +
|
| 33 | +let n = 10; |
| 34 | +
|
| 35 | +console.log(n.toString());//10 |
| 36 | +
|
| 37 | +console.log(n.toString(8));//12 |
| 38 | +
|
| 39 | +
|
| 40 | +4. |
| 41 | +function a() { |
| 42 | + console.log('hai'); |
| 43 | + |
| 44 | +} |
| 45 | +
|
| 46 | +console.log(a());//hai undefined |
| 47 | +
|
| 48 | +
|
| 49 | +
|
| 50 | +function a() { |
| 51 | + console.log('hai'); |
| 52 | + return 10 |
| 53 | +} |
| 54 | +
|
| 55 | +console.log(a());//hai 10 |
| 56 | +
|
| 57 | +
|
| 58 | +
|
| 59 | +5. function abc() { |
| 60 | + console.log('xyz'); |
| 61 | +} |
| 62 | +
|
| 63 | +console.log(abc.name); // abc |
| 64 | +
|
| 65 | +
|
| 66 | +6. |
| 67 | +console.log(abc)//Ref Error |
| 68 | +
|
| 69 | +
|
| 70 | +
|
| 71 | +7. |
| 72 | +Why the Difference? |
| 73 | +var: Variables declared with var are function-scoped or globally-scoped |
| 74 | + (if declared outside any function). When declared in the global scope, |
| 75 | + they are added to the window object in browsers. |
| 76 | +
|
| 77 | +let and const: Variables declared with let or const have block scope |
| 78 | +(limited to the block, statement, or expression in which they are defined) and are not added to the global object. |
| 79 | +
|
| 80 | +var abc = 100; |
| 81 | +let xyz = 200; |
| 82 | +
|
| 83 | +console.log(window.xyz); // undefined REF error |
| 84 | +console.log(window.abc); // 100 |
| 85 | +
|
| 86 | +
|
| 87 | +
|
| 88 | +8. |
| 89 | +
|
| 90 | +Key Points: |
| 91 | +Strict mode makes arguments and function parameters (n1, n2) independent, meaning changes to parameters don't |
| 92 | +reflect in the arguments object. |
| 93 | +The arguments object holds the original values passed to the function (100 and 200), |
| 94 | + even though you modified the parameters inside the function. |
| 95 | +Let me know if you'd like further clarification! |
| 96 | +
|
| 97 | +function abc(n1, n2) { |
| 98 | + 'use strict' |
| 99 | + n1 = 300; |
| 100 | + n2 = 400; |
| 101 | + return arguments[0] + arguments[1]; |
| 102 | +} |
| 103 | +
|
| 104 | +console.log(abc(100, 200));//300 |
| 105 | +
|
| 106 | +
|
| 107 | +
|
| 108 | +
|
| 109 | +function abc(n1, n2) { |
| 110 | + |
| 111 | + n1 = 300; |
| 112 | + n2 = 400; |
| 113 | + return arguments[0] + arguments[1]; |
| 114 | +} |
| 115 | +
|
| 116 | +console.log(abc(100, 200));//700 |
| 117 | +
|
| 118 | +9. new xyz();//Ref Error |
| 119 | +
|
| 120 | +function abc() { |
| 121 | + console.log('hai'); |
| 122 | +} |
| 123 | +
|
| 124 | +class xyz { |
| 125 | +} |
| 126 | +
|
| 127 | +
|
| 128 | +
|
| 129 | +10. const abc = 10 + isNaN(2) ? 100 : 200;//10+0?100:200 10?100:200 |
| 130 | +console.log(abc);//100 |
| 131 | +
|
| 132 | +
|
| 133 | +
|
| 134 | +11. |
| 135 | +console.log(10 == 10); // true |
| 136 | +console.log(10 == 10 == 10); // false true==10 1==10 |
| 137 | +console.log(10 == 10 == 0); // false 1==0 |
| 138 | +
|
| 139 | +
|
| 140 | +
|
| 141 | +12. |
| 142 | +
|
| 143 | +console.log(parseInt(''))//NAN |
| 144 | +console.log(Number(''))//0 |
| 145 | +console.log(parseInt("Nil"))//NAN |
| 146 | +
|
| 147 | +console.log(parseInt(true))//NAN |
| 148 | +console.log(parseInt(false))//NAN |
| 149 | +
|
| 150 | +console.log(Number(true))//1 |
| 151 | +console.log(Number(false))//0 |
| 152 | +
|
| 153 | +*/ |
| 154 | + |
| 155 | + |
| 156 | + |
0 commit comments