Member-only story
Object-Oriented JavaScript — Conditionals and Loops
3 min readSep 15, 2020
JavaScript is partly an object-oriented language.
To learn JavaScript, we got to learn the object-oriented parts of JavaScript.
In this article, we’ll look at conditionals and loops.
Ternary Operator
The ternary operator is a shorter version of the if-else syntax.
For instance, instead of writing:
let a = 3;
let result = '';
if (a === 1) {
result = "a is 3";
} else {
result = "a is not 3";
}
We can write:
let a = 1;
let result = (a === 3) ? "a is 3" : "a is not 3";
The ternary expression can be added with the ?
and :
symbols.
a === 3
is the conditional expression.
And the strings are what we return.
Switch
If we have lots of if
conditions and else...if
parts, then we can use the switch
statements to write them,
For instance, we can write:
let a = '1',
result = '';
switch (a) {
case 1:
result = 'Number';
break;
case '1':
result = 'String';
break;
default:
result = ''
break;
}