pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/Hyuk/javascript/blob/master/javascript-basics/javascript-basics/condition.md

68dd150ce6c8e711.css" /> javascript/javascript-basics/javascript-basics/condition.md at master · Hyuk/javascript · GitHub
Skip to content

Latest commit

 

History

History
116 lines (94 loc) · 1.64 KB

File metadata and controls

116 lines (94 loc) · 1.64 KB

JavaScript Conditions

  • if else
  • ternary operator
  • switch
  • while loop
  • for loop
  • nested for loop
  • do while loop

if else

var num1 = 6;

if (num1 < 5) {
    console.log("the number is less than 5");
} else if (num1 == 5) {
    console.log("the number is 5");
} else {
    console.log("the number is greater than 5");
}

// the number is greater than 5

ternary operator

function checkEqual(a, b) {
  return a == b ? true : false;
}

checkEqual(1, 2); // false

function checkSign(num) {
  return num > 0 ? "positive" : num < 0 ? "negative" : "zero"
}

checkSign(10); // positive

switch

var num1 = 3;

switch(num1) {
    case 1:
        console.log("the number is one");
        break;
    case 2:
        console.log("the number is two");
        break;
    case 3:
        console.log("the number is three");
        break;
    default:
        console.log("exit");
}

// the number is three

while loop

var myArray = [];

var i = 0;
while(i<5) {
    myArray.push(i);
    i++;
}
console.log(myArray);

// [0,1,2,3,4]

for loop

var numArray = [];

for (var i = 1; i<5; i++) {
    numArray.push(i);
}
console.log(numArray);

// [1,2,3,4]

nested for loop

function multiplyAll(arr) {
  var product = 1;
  for (var i = 0; i < arr.length; i++) {
    for (var j = 0; j < arr[i].length; j++) {
      product *= arr[i][j];
    }
  }
  return product;
}

multiplyAll([[1,2],[3,4],[5,6,7]]);

// 5040

do while loop

var myArray = [];
var i = 10;

do {
    myArray.push(i);
    i++;
} while (i < 11);
console.log(myArray);

// [10]
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy