JavaScript Conditional Statements
Conditional statements are used in programming to perform different actions based on whether certain conditions are met or not. This allows control and decision making in the execution of the program. JavaScript has several different types of conditional statements, but the most common ones are if statements and switch statements.
if statements
if statements are used to check if a certain condition is true and to execute specific code accordingly. For example, you can use if statements to check if a variable is equal to another:
1var x = 5;
2var y = 10;
3if (x == y) {
4 console.log("Variables are equal");
5} else {
6 console.log("Variables are not equal");
7}This checks if the variables "x" and "y" are equal. If they are, the code executes the first sentence, otherwise the second sentence.
Learn to hack — start here
Hundreds of interactive courses, virtual labs and CTF challenges in your browser. Start a free trial — no card required.