Welcome to the JavaScript Statements tutorial! In this comprehensive guide, we'll explore the fundamental building blocks of JavaScript programming: statements. By the end of this lesson, you'll have a solid understanding of how to write, execute, and debug JavaScript statements. Let's get started!
In the world of programming, a statement is a line of code that performs an action or makes a declaration. JavaScript statements allow us to instruct the browser to perform various tasks, such as creating variables, performing calculations, and making decisions.
Let's start with the most basic JavaScript statement: variable declaration. Variables are containers for storing values that we can use later in our code.
// Declare a variable
let myVariable;
// Assign a value to the variable
myVariable = 5;
// Output the value of the variable
console.log(myVariable); // 5š Note: In JavaScript, we have three ways to declare variables: let, const, and var. We'll cover each of them later in this tutorial.
Now that we have a variable, let's perform some calculations! JavaScript supports the following arithmetic operations:
+)-)*)/)%)++)--)// Arithmetic operations example
let num1 = 5;
let num2 = 3;
// Addition
let sum = num1 + num2; // 8
// Subtraction
let difference = num1 - num2; // 2
// Multiplication
let product = num1 * num2; // 15
// Division
let quotient = num1 / num2; // 1.6666666666666667
// Modulus
let remainder = num1 % num2; // 1
// Increment
num1++; // 6
// Decrement
num1--; // 5Comparison operators allow us to compare two values and determine their relationship. JavaScript supports the following comparison operators:
==)!=)>)<)>=)<=)// Comparison operators example
let num1 = 5;
let num2 = 3;
// Equal to
let isEqual = num1 == num2; // false
// Not equal to
let isNotEqual = num1 != num2; // true
// Greater than
let isGreaterThan = num1 > num2; // true
// Less than
let isLessThan = num1 < num2; // false
// Greater than or equal to
let isGreaterThanOrEqual = num1 >= num2; // false
// Less than or equal to
let isLessThanOrEqual = num1 <= num2; // trueLogical operators allow us to combine multiple conditional statements and make more complex decisions. JavaScript supports the following logical operators:
&&)||)!)// Logical operators example
let num1 = 5;
let num2 = 3;
// AND (`&&`)
let isBothGreaterThan3 = (num1 > 3) && (num2 > 3); // true
// OR (`||`)
let isEitherGreaterThan3 = (num1 > 3) || (num2 > 3); // true
// NOT (`!`)
let isNotBothGreaterThan3 = !isBothGreaterThan3; // falseControl flow statements allow us to control the flow of execution in our code. We'll cover the following control flow statements in this tutorial:
if statementselse statementselse if statementsswitch statementsfor, while, do-while)What is the result of the following code?
Stay tuned for our next lesson on JavaScript Control Flow Statements! š