mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-02-26 17:01:40 -05:00
feat(curriculum): add interactive examples to javascript comparisons and conditionals (#65327)
Co-authored-by: Jeevankumar-S <jeevenkumar2003@email.com>
This commit is contained in:
@@ -5,42 +5,60 @@ challengeType: 31
|
||||
dashedName: review-javascript-comparisons-and-conditionals
|
||||
---
|
||||
|
||||
# --description--
|
||||
# --interactive--
|
||||
|
||||
## Comparisons and the `null` and `undefined` Data Types
|
||||
|
||||
- **Comparisons and `undefined`**: A variable is `undefined` when it has been declared but hasn't been assigned a value. It's the default value of uninitialized variables and function parameters that weren't provided an argument. `undefined` converts to `NaN` in numeric contexts, which makes all numeric comparisons with `undefined` return `false`.
|
||||
|
||||
:::interactive_editor
|
||||
|
||||
```js
|
||||
console.log(undefined < 0); // false (NaN < 0 is false)
|
||||
console.log(undefined >= 0); // false (NaN >= 0 is false)
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
- **Comparisons and `null`**: The `null` type represents the intentional absence of a value. `null` converts to `0` in numeric contexts, which may result in unexpected behavior in numeric comparisons:
|
||||
|
||||
:::interactive_editor
|
||||
|
||||
```js
|
||||
console.log(null < 0); // false (0 < 0 is false)
|
||||
console.log(null >= 0); // true (0 >= 0 is true)
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
- When using the equality operator (`==`), `null` and `undefined` only equal each other and themselves:
|
||||
|
||||
:::interactive_editor
|
||||
|
||||
```js
|
||||
console.log(null == undefined); // true
|
||||
console.log(null == 0); // false
|
||||
console.log(undefined == NaN); // false
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
- However, when using the strict equality operator (`===`), which checks both value and type without performing type coercion, `null` and `undefined` are not equal:
|
||||
|
||||
:::interactive_editor
|
||||
|
||||
```js
|
||||
console.log(null === undefined); // false
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
## `switch` Statements
|
||||
|
||||
- **Definition**: A `switch` statement evaluates an expression and matches its value against a series of `case` clauses. When a match is found, the code block associated with that case is executed. A `break` statement should be placed at the end of each case, to terminate its execution and continue with the next. The `default` case is an optional case and only executes if none of the other cases match. The `default` case is placed at the end of a `switch` statement.
|
||||
|
||||
:::interactive_editor
|
||||
|
||||
```js
|
||||
const dayOfWeek = 3;
|
||||
|
||||
@@ -71,6 +89,8 @@ switch (dayOfWeek) {
|
||||
}
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
# --assignment--
|
||||
|
||||
Review the JavaScript Comparisons and Conditionals topics and concepts.
|
||||
|
||||
Reference in New Issue
Block a user