Skip to content

Commit 10afe78

Browse files
committed
q2 replaced thanks to @fssq1993 and @yairEO
1 parent c9929e4 commit 10afe78

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

README.md

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,30 +43,21 @@ console.log(y); // Output: ReferenceError: y is not defined
4343
[http://stackoverflow.com/questions/20822022/javascript-variable-definition-declaration](http://stackoverflow.com/questions/20822022/javascript-variable-definition-declaration)
4444

4545

46-
## Question 2. What will be the output of the following code?
46+
## Question 2. For which value of `x` the results of the following statements are not the same?
47+
4748

4849
```javascript
49-
var y = 1;
50-
if (function f() {}) {
51-
y += typeof f;
52-
}
53-
console.log(y);
50+
// if( x <= 100 ) {...}
51+
if( !(x > 100) ) {...}
5452
```
5553
### Answer
5654

57-
The code above would give the output `1undefined`. inside `if` statement we have `function f(){}`. Here's it's a function expression, not function statement, which is why f is not a variable.
55+
`NaN <= 100` is `false` and `NaN > 100` is also `false`, so if the
56+
value of `x` is `NaN`, the statements are not the same.
5857

59-
As a reminder, function expression is something like this:
60-
```javascript
61-
var myFunc1 = new function() {}
62-
var myFunc2 = new function whatever() {} // whatever is not a variable here
63-
```
58+
The same holds true for any value of x that being converted to Number, returns NaN, e.g.: `undefined`, `[1,2,5]`, `{a:22}` , etc.
6459

65-
A function statement is something like this:
66-
```javascript
67-
function myFunc3() {}
68-
```
69-
You can read about [function expressions](https://developer.mozilla.org/en-US/docs/web/JavaScript/Reference/Operators/function) and [function statements](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function) on MDN.
60+
This is why you need to pay attention when you deal with numeric variables. `NaN` can’t be equal, less than or more than any other numeric value, so the only reliable way to check if the value is `NaN`, is to use `isNaN()` function.
7061

7162
## Question 3. What is the drawback of having private methods in a JavaScript object?
7263

0 commit comments

Comments
 (0)