Javascript Notes: 04 – Operators

In Javascript we have different kinds of Operators. Along with operators we use variables to create expressions. And with expressions we can implement logic and Algorithms.

An algorithm is: an ordered set of instructions recursively applied to transform data input into processed data output, as a mathematical solution, descriptive statistics, internet search engine result, or predictive text suggestions.

Logic is: All digital computers are based on a two-valued logic system—1/0, on/off, yes/no (see binary code). Computers perform calculations using components called logic gates, which are made up of integrated circuits that receive an input signal, process it, and change it into an output signal.

We have these different kinds of operators in Javascript:

  • Arithmetic
  • Assignment
  • Comparison
  • Logical
  • Bitwise
Arithmetic Operator:
 
Example:
let x = 10;
let y = 3;
 
console.log(x + y);    //addition – would be 13
console.log(x – y);     //subtraction – would be 7
console.log(x * y);    //multiplication – would be 30
console.log(x / y);    //division – would be 3.3333333333335
console.log(x % y);   //remainder of division – would be 1
console.log(x ** y);    //x to the power of y – would be 1000
 
//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)
Example:
let x = 10;
 
x = x+5;  //Long hand  = 15
x += 5;   //Short hand  = 15
 
x = x * 3;  //Long hand = 30
x *= 3;   //Short hand  = 30
 
Comparison Operator
Example:
let x = 1;
 
//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
Example:
//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)
 

Boolean –  a data type having two possible values representing “true” or “false.”

 
Example 1:
//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
 
 
Example 2A: (An example of use of the logical operator in real world.)
This will test if the person has high income and credit score to test the loan.
let highIncome = true;
let goodCreditScore = true;
let eligibleForLoan = highIncome && goodCreditScore;
 
console.log(eligibleForLoan);
Example 2B:
//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
Example 2C: Now we add in the NOT (!)
 
//Logical or (||)
//Returns TRUE if one of the operands is TRUE. Just takes one TRUE to be TRUE!
let highIncome = false;
let goodCreditScore = false;
let eligibleForLoan = highIncome || goodCreditScore;
console.log(‘Eligible’, eligibleForLoan); 
 
//NOT (!)  – This operator will give us the opposite.
let applicationRefused =  !eligibleForLoan;
 
console.log(‘Application Refused’, applicationRefused);
Logical Operator (with NON Boolean) – Truthy or Falsy
In this example 1 we are using the console in Chrome. The Javascript engine tries to evaluate these expressions. If the operand is not a Boolean true or false it will try to interpret it as Truthy or Falsy.
 
Example 1:
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!
A real world example of when to use this. A user has to pick a color or we have a default color. This is the power of using Non-Booleans!
let userColor = ‘red’
let defaultColor = ‘blue’;
let currentColor = userColor || defaultColor;
console.log(currentColor);  //The answer we get is red.
Now look at this way:
let userColor = ‘undefined’ //The user has to pick a color, if not we go to default.
let defaultColor = ‘blue’;
let currentColor = userColor || defaultColor;
 
console.log(currentColor); //The answer we get is blue.
 
 
Operators Precedence
 
Example 1:
 
let x = 2 + 3 * 4
console.log(x); //We get 14
Example 2:
 
let x = (2 + 3) * 4
console.log(x); //We get 20
 
 

Exercise:

 
Have on the console this problem show blue then red.
 

let a = ‘red’;
let b = ‘blue’;

missing code

console.log(a);
console.log(b);
 
 

 

===Answer===


let a = ‘red’;
let b = ‘blue’;
let c = a;
a = b;
b = c;


console.log(a);
console.log(b
);