Operators and loops Flashcards
List several common arithmetic operators
+ a = c + b
% a = b % c
* a = b * c
++ a++ would add 1 to a
– a– would subtract 1 from a
+= a += b would add b to a
-= a -= b would subtract b from a
*= a *= b multiplies a by b
/= a /= b divides a by b
What are some common string operators
+ “string1” + “string”
${} let name = ‘${firstName} ${lastName}’;
what is the difference between == and ===
A == B cpmpares the values of A and B
A === B compares the value and type of A and B
How is not equal value and not equal value and type written?
not equal value A != B
not equal value and type A !== B
Write an example of each of the three logical operators used in JS
A === 10 && B === 20
where && is “and”
A === 15 !! B === 5
where !! is “and”
!( a === b ) where ! is “not”
What are the conditional operators used in JS
If else elseif endif
What is te syntax of a conditional statement after the If else or elseif
The statement to be executed are enclosed in curly brackets. For example
If ( a <= 100 {
do this( )
}
else {
do this ( )
}
What is the ternary operator
let a =100
let output =
a <= 100
? ‘${a} is less than 100
: ‘${a} is not less than 100’;
console.log(output);
so if the statement is true everything after the ? runs
if the statement is false everything after : runs even if it is another if statement
How are Ternary operators chained?
let a = 100
let output =
a <= 100? ‘${a} is less than 100’
: a >= 100
? ‘{a} is greater than 99’
: ‘{a} is less than 100
ive an example of the SWITCH statement in JS
let name = ‘Bob’;
switch (name) {
case ‘Bill” :
console.log(“it is Bob”);
case “Google”:
console.log(“it is Google”;
case “Bob”:
console.log(“it is Bob”);
default:
console.log({name});
What are the loop types in JS?
Do
Do While
For
List a simple FOR loop
for (let counter = 0; counter <= 10; counter++) {
console.log(counter);
}
List a simple do while loop
let count = 0;
do {
console.log(count);
count += 1;
} while count <= 10);
list a simple while loop
let cnt = 0;
while (cnt <= 10) {
console.log(cnt);
count ++;
}