java
java
00;
Logical operators
Logical operators can be used in conjunction with boolean values
(true and false) to create complex logical expressions.
Operat Meanin
Example How it works
or g
Logical value1 &&
&&
value2
Returns true if both value1 and value2 evaluate to true.
AND
value1 || Returns true if either value1 or value2 (or even both!)
|| Logical OR value2
evaluates to true.
Logical Returns the opposite of value1. If value1 is true, then !
! !value1
NOT value1 is false
TIP: Using if(isGoing) is the same as using if(isGoing === true) . Alternatively,
using if(!isGoing) is the same as using if(isGoing === false) .
Ternary operator
The ternary operator provides you with a shortcut alternative for
writing lengthy if...else statements.
If you breakdown the code, the condition isGoing is placed on the left
side of the ?. Then, the first expression, after the ?, is what will be
run if the condition is true and the second expression after the, :, is
what will be run if the condition is false.
const option = 3;
switch (option) {
case 1:
console.log("You selected option 1.");
break;
case 2:
console.log("You selected option 2.");
break;
case 3:
console.log("You selected option 3.");
break;
case 4:
console.log("You selected option 4.");
break;
case 5:
console.log("You selected option 5.");
break;
case 6:
console.log("You selected option 6.");
break; // technically, not needed
}
Prints: You selected option 3.
const option = 23;
switch (option) {
case 1:
console.log("You selected option 1.");
break;
case 2:
console.log("You selected option 2.");
break;
case 3:
console.log("You selected option 3.");
break;
case 4:
console.log("You selected option 4.");
break;
case 5:
console.log("You selected option 5.");
break;
case 6:
console.log("You selected option 6.");
break;
default:
console.log("You did not select a valid option.");
}
Along the way we'll give you a lot of practice writing loops.
// Add y to x
x += y // x = x + y
// Subtract y from x
x -= y // x = x - y
// Multiply x by x
x *= y // x = x* y
// Divide x by y
x /= y // x = x / y
These assignment operators will come in handy as you create more
loops!
What does this code log out? javascript let x = 4; x++; console.log(x);
function reverseString(reverseMe) {
let reversed = "";
for (let i = reverseMe.length - 1; i >= 0; i--) {
reversed += reverseMe[i];
}
return reversed;
}
console.log(reverseString("Julia"));
Prints "ailuJ"
Let's break it down:
Annotated Function
donuts.forEach(function(donut) {
donut += " hole";
donut = donut.toUpperCase();
console.log(donut);
});