JavaScript Statements code
Mark
JavaScript statements are used to perform actions and make decisions in a program. Here are some common JavaScript statements:
- Variable declaration: Used to declare a variable and assign it a value.
var x = 5;
- Conditional statement: Used to execute different code blocks based on a condition.
if (x > 10) { console.log('x is greater than 10'); } else { console.log('x is less than or equal to 10'); }
- Looping statement: Used to execute a block of code multiple times.
for (var i = 0; i < 10; i++) { console.log(i); }
- Function declaration: Used to define a function that can be called later in the program.
function add(x, y) { return x + y; }
- Switch statement: Used to execute different code blocks based on the value of a variable.
switch (day) { case 'Monday': console.log('Today is Monday'); break; case 'Tuesday': console.log('Today is Tuesday'); break; default: console.log('Today is not Monday or Tuesday'); }
These are just a few examples of the types of statements you can use in JavaScript. There are many more statements available that can help you perform different actions and make decisions in your program.