TIL 13: javascript 4

Standard

Pressing on with freeCodeCamp! Start at the beginning here or previous installment.

Booleans:

  • true or false
  • nb NOT “true” and “false”

Conditional logic:

code

  • ‘myCondition’ above could also be a comparison operate that equates to true or false. E.g. if(myVal == 10); where ‘==’ is the equality operator (nb do not use assignment ‘=’ by mistake!) or if(thatVal > 9);
  • note that javascript will attempt to convert data types. Thus, 1 == ‘1’ and “3” == 3
  • to get past this conversion, use strict equality to test for value and type equality e.g. 3 === 3 is true, but 3 === ‘3’ is false
  • the inequality operator is ‘!=’, and likewise ‘!==’ for strict inequality
  • logical AND operator ‘&&’ allows two conditions to be tested, with a ‘true’ result requiring both to be met
  • logical OR operator ‘||’ returns true if either condition is met
  • else statements allow for alternate code paths to be triggered e.g. if x do y; else do z;
  • else if allows the chaining of multiple if statements e.g. if a do x, else if b do y, else do z;

Switch statements:

code

  • tests a value and can have many case statements defining various outcomes
  • can be easier to write than chained if/else if statements
  • cases are tested with strict (===) equality
  • a default option can/should be provided for when none of the cases are true
  • multiple cases can evaluate to the same outcome by omitting a break statement, e.g. case 1: case 2: case 3: result = “1, 2 or 3”; break;

Returning Boolean Values (e.g. from Functions):

  • instead of if (a === b) {return true }; else {return false}; you can simply write return a === b; since this returns true or false automatically
Advertisements

One response »

  1. Pingback: TIL 16: javascript 5 | diary of a scanner

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s