Commit 1b9a56e0 authored by MohammadAli Keshavarz's avatar MohammadAli Keshavarz

The Ternary Operator and Switch Statements

parent d82e8fd3
......@@ -133,7 +133,7 @@ if (BMIMark > BMIJohn) {
/*****************************
* Boolean logic
*/
/*
var firstName = 'John';
var age = 20;
if (age < 13) {
......@@ -145,3 +145,56 @@ if (age < 13) {
} else {
console.log(firstName + ' is a man.');
}
*/
/*****************************
* The Ternary Operator and Switch Statements
*/
var firstName = 'John';
var age = 14;
// Ternary operator
age >= 18 ? console.log(firstName + ' drinks beer.') : console.log(firstName + ' drinks juice.');
var drink = age >= 18 ? 'beer' : 'juice';
console.log(drink);
(if (age >= 18) {
var drink = 'beer';
} else {
var drink = 'juice';
}
// Switch statement
var job = 'instructor';
switch (job) {
case 'teacher':
case 'instructor':
console.log(firstName + ' teaches kids how to code.');
break;
case 'driver':
console.log(firstName + ' drives an uber in Lisbon.');
break;
case 'designer':
console.log(firstName + ' designs beautiful websites.');
break;
default:
console.log(firstName + ' does something else.');
}
age = 56;
switch (true) {
case age < 13:
console.log(firstName + ' is a boy.');
break;
case age >= 13 && age < 20:
console.log(firstName + ' is a teenager.');
break;
case age >= 20 && age < 30:
console.log(firstName + ' is a young man.');
break;
default:
console.log(firstName + ' is a man.');
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment