Java Script Command Cheat Sheet

Java Script Command Cheat Sheet

typeof – Use this to check if item is a string, variable, array, object

console.log( ) – display results in console screen.

 

Arithmetic Operator:
+ y    //addition 
–      //subtraction 
*     //multiplication 
/     //division 
% ;  //remainder of division
(x ** y);    //x to the power of y
 
//Increment (++)
console.log(++x);    //would be 11
 
console.log(x++);   // you would see 10 first in console then add 1
console.log(x);
 
//Decrement (–)
console.log(–y);  //would be 2
 
Assignment Operator: (A shorthand for arithmetic)
x = x+5;  //Long hand  = 15
x += 5;   //Short hand  = 15
 
x = x * 3;  //Long hand = 30
x *= 3;   //Short hand  = 30
 
Comparison Operator
 
//Relational
console.log(x > 0);  //greater than (true)
console.log(x >= 1);  //greater than or equal (true)
console.log(x < 1);   //less than (false)
console.log(x <=1);   //less than or equal (true)
 
//Equality
console.log(x === 1);  // is x equal to value (true)
console.log(x !==1);   // is x not equal to value (false)
 
Equality Operator
//Strict Equality (Type + Value)
console.log(1 === 1);    // True
console.log(‘1’ === 1);   // False
 
// Lose Equality (Value)
console.log(1 == 1);    // True
console.log(‘1’ == 1);   // True
console.log(true == 1);   // True
 
 
Logical Operator (Boolean)
//Logical AND (&&)
// Returns TRUE if both operands are TRUE
console.log(true && true);   //This one shows TRUE
console.log(false && true);   //This one shows FALSE
console.log(true && false);  //This one shows FALSE
//Logical or (||)
//Returns TRUE if one of the operands is TRUE. Just takes one TRUE to be TRUE!
let highIncome = false;
let goodCreditScore = true;
let eligibleForLoan = highIncome || goodCreditScore;
 
console.log(eligibleForLoan); //Answer comes out TRUE
Logical Operator (with NON Boolean) – Truthy or Falsy
false || true         //True
false || ‘Mosh’     //”Mosh”
false || 1             //1
What are these Falsy (false) values? Here we go:
  • undefined
  • null
  • 0 (Number zero)
  • false (Boolean)
  • ‘ ‘ (Empty string)
  • Nan (Not a number) (Is a special value in JavaScript that does not produce a valid number.)
If we use any of these Falsy values in a logical expression then they will be treated as Falsy.
Anything that is not Falsy is then Truthy.
This is an example of what we call short-circuiting using || in the console on Chrome.
false || 1 || 2 || 4 || true  – The answer is 1. Javascript ignores 2,4, and true!

You May Also Like