Skip to content

promahbubul/JavaScript_Note_3_Conditional-Operators

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

JavaScript Conditional Operators

If Statements
if (true) {
  console.log("It will be printed");
}
if (false) {
  console.log("It will be printed");
}
let sale = true;
if (sale) {
  console.log("Time to  buy");
}
let sale = true;
sale = false;
if (sale) {
  console.log("Time to  buy");
}
If...Else Statements
if (false) {
  console.log("This code with no run");
} else {
  console.log("This code will run");
}
if (true) {
  console.log("This code with no run");
} else {
  console.log("This code will run");
}
let sale = true;
sale = false;
if (sale) {
  console.log("Time to buy");
} else {
  console.log("Time to wait");
}
Comparison Operators
1) Less than

<

let hungerLevel = 7;
if (hungerLevel < 7) {
  console.log("Time to eat");
} else {
  console.log("Time to wait");
}
2) Geater than

>

let hungerLevel = 7;
if (hungerLevel > 7) {
  console.log("Time to eat");
} else {
  console.log("Time to wait");
}
3) Less than or equal to

<=

let hungerLevel = 7;
if (hungerLevel <= 7) {
  console.log("Time to eat");
} else {
  console.log("Time to wait");
}
4) Geater than or equal to

>=

let hungerLevel = 7;
if (hungerLevel >= 7) {
  console.log("Time to eat");
} else {
  console.log("Time to wait");
}
5) Is equal to

===

let hungerLevel = 7;
if (hungerLevel === 7) {
  console.log("Time to eat");
} else {
  console.log("Time to wait");
}
6) Is NOT equal to

!==

let hungerLevel = 7;
if (hungerLevel !== 7) {
  console.log("Time to eat");
} else {
  console.log("Time to wait");
}
Logical Operators

There are 3 Logical Operators.

AND &&
OR ||
NOT !

1) AND Operator

&&

let stopLight = "green";
let pedestrians = 0;
if (stopLight === "green" && pedestrians === 0) {
  console.log("Go!");
} else {
  console.log("Stop");
}
let stopLight = "red";
let pedestrians = 0;
if (stopLight === "green" && pedestrians === 0) {
  console.log("Go!");
} else {
  console.log("Stop");
}
let mood = "sleepy";
let triednessLevel = 5;
if (mood === "sleepy" && triednessLevel === 8) {
  console.log("Time to Sleep");
} else {
  console.log("Not bed time yet");
}
2) OR Operator

||

let stopLight = "green";
let pedestrians = 0;
if (stopLight === "green" && pedestrians === 0) {
  console.log("Go!");
} else {
  console.log("Stop");
}
let stopLight = "red";
let pedestrians = 0;
if (stopLight === "green" && pedestrians === 0) {
  console.log("Go!");
} else {
  console.log("Stop");
}
let stopLight = "red";
let pedestrians = 1;
if (stopLight === "green" && pedestrians === 0) {
  console.log("Go!");
} else {
  console.log("Stop");
}
3) NOT Operator

!

let excited = true;
if (!excited) {
  console.log("I am excited");
} else {
  console.log("I am not excited");
}
let mood = "sleepy";
let triednessLevel = 5;
if (!(mood === "sleepy" && triednessLevel === 8)) {
  console.log("Time to Sleep");
} else {
  console.log("Not bed time yet");
}
Truthy and Falsy
  • when let string
let myValue = "value";
if (myValue) {
  console.log(myValue);
} else {
  console.log("The variable is not initialize");
}
  • when let Number 0
let myValue = 0;
if (myValue) {
  console.log(myValue);
} else {
  console.log("The variable is not initialize");
}
  • when let Empty String ''
let myValue = "";
if (myValue) {
  console.log(myValue);
} else {
  console.log("The variable is not initialize");
}
  • when let Number 1
let myValue = 1;
if (myValue) {
  console.log(myValue);
} else {
  console.log("The variable is not initialize");
}
  • when let null
let myValue = null;
if (myValue) {
  console.log(myValue);
} else {
  console.log("The variable is not initialize");
}
let username = "Bod";
let defaultName;
if (username) {
  defaultName = username;
} else {
  defaultName = "Strange";
}
console.log(defaultName);
let username;
let defaultName;
if (username) {
  defaultName = username;
} else {
  defaultName = "Strange";
}
console.log(defaultName);
  • short-circuit evaluation
let username;
let defaultName = username || "Stringer";
console.log(defaultName);
let username = "Bod";
let defaultName = username || "Stringer";
console.log(defaultName);

Exercise:

let tool;
let writingTool = tool || "Pen";
console.log(`The is ${writingTool} mightier than the sword`);
let tool = "Marker";
let writingTool = tool || "Pen";
console.log(`The is ${writingTool} mightier than the sword`);
Ternary Operator
  • If...Else Statement Operator
let isNightTime = true;
  isNightTime?console.log('Turn on the lights!');
  :console.log('Turn off the lights!');

To Converted Tarnary Operator

  • when let true
let isNightTime = true;
  isNightTime?console.log('Turn on the lights!');
  :console.log('Turn off the lights!');
  • when let false
let isNightTime = false;
  isNightTime?console.log('Turn on the lights!');
  :console.log('Turn off the lights!');

Exercise:

  • If...Else Statement Operator
let temp = 40;
if (temp < 35) {
  console.log("It is hot");
} else {
  console.log("it is OK");
}

To Converted Tarnary Operator

temp < 35 ? console.log("It is hot") : console.log("It is OK");
Else If
let stopLight = "yellow";
if (stopLight === "red") {
  console.log("Stop!");
} else if (stopLight === "yellow") {
  console.log("slow down!");
} else if (stopLight === "green") {
  console.log("Go!");
} else {
  console.log("Caution, unlock");
}
The switch keyword
let groceryItem = "Alo";

switch (groceryItem) {
  case "Tomato":
    console.log("Tomatos are $0.49");
    break;
  case "Alo":
    console.log("Alos are $2.35");
    break;
  case "Potato Chipe":
    console.log("Potatos are $0.40");
    break;
  default:
    console.log("Invalid item");
    break;
}

About

JavaScript Conditional Operators

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages