Bo dinglesTM - -   Bright Lightbulb Graphic Ideas   - -

The Web Developer Bootcamp 2024

Colt Steele

Back to JavaScript Index Page


JavaScript Decision Making

JavaScript Decision Making PDF

Equality

JavaScript decision making basically involves branching to different code outputs based on some form of logical comparisons that return Boolean Logic values of True or False. If this logic is true then branch to this code, if not then branch to different code. These logical comparisons and done using different Comparison Operators.

All letters, numbers, and characters are assign a Unicode number that you can use to compare with. If you are comparing A to a, you are not comparing two letters, instead you are comparing two unicode numbers.

For a complete Unicode List, Click Here

So if you are comparing A to a you are actually comparing 41(A) to 61(a). So if you compare is A > a, you are actually comparing is 41 > 61 which is False. So the comparison is A > a evaluates to False.

Basic Operators

  • > : Greater Than
  • < : Less Than
  • >= : Greater Than or Equal To
  • <= : Less Than or Equal To

Equality Operators

  • == : Equal
    • ∴ Checks for equality of value, not equality of type.
    • ∴ This coerses both values to the same type and then compares them.
    • ∴ This can possibly lead to some very unexpected results.
    • ∴ 1 == "1" would evaluate to TRUE even though they are two different types, 1 is a number and "1" is a string.
  • != : Not Equal
    • ∴ Checks for equality of value, not equality of type.
  • === : Strictly Equal
    • ∴ Checks for equality of value, AND equality of type.
    • ∴ 1 === 1 would evaluate to TRUE - They are both equal in value, and equal in type.
    • ∴ 1 === "1" would evaluate to FALSE - They are equal value, but not equal in type.
  • !== : Strictly non-Equal
    • ∴ Checks for equality of value, AND equality of type.

The overall takeaway here is that for most instances, you should use === and !==.


Truth & False Values

All JavaScript values have an inherit Truth or False about them

False Values

  • false
  • 0
  • "" (empty string)
  • Null
  • Undefined
  • NaN

True Values

  • Everything Else!


Conditionals

Conditionals are logic test's that are performed to determine if an argument is true or false. Decisions can then be made that control the flow of the code based of the result of the true/false comparison.

The control of the flow of the code rests withing the following Conditional statements:

  • if: specify a block of code to be executed, if a specified condition is true
  • else: specify a block of code to be executed, if a specified condition is false
  • else if: specify a new condition to test, if the first condition is false
  • switch: selects one of many code blocks to be executed
    • - The switch expression is evaluated once
    • - The value of the expression is compared with the values of each case
    • - If there is a match, the associated block of code is executed
    • - If there is no match, the default code block is executed
    • - W3Schools | Switch Statement


Advanced Logical Operators

and ( && ): both expressions must be true for the entire statement to be true

  • if (var1 === 5 && var2 === 5) : true
  • if (1 < 4 && 'a' === 'a') : true
  • if (9 < 10 && 9 <= 10) : false
  • if ('abc'.length === 3 && 1 + 1 === 4) : false

or ( || ): if either expression is true, the entire statement is true

  • if (1 < 4 || 'a' === 'b') : true
  • if (9 > 10 || 9 <= 10) : true
  • if ('a' === 'b' || 5 === 6) : false
  • if (0 || undefined) : false

Order of priority :: and ( && ) always has priority over or ( || ) and will be evaluated first.


Not

not ( ! ): !expression returns true, if expression is false. Essentially returns the opposite of how an expression evaluates. In the following examples,

  • !null : true : null is false, so not null returns true
  • !(0 === 0) : false : 0 does equal 0 (true) so not (0 = 0) returns false
  • !(3 <= 4) : false : 3 is less than 4 , so true but not is 3 <= 4 returns false


Back to Top