JS Statements šŸŽÆ

beginner
12 min

JS Statements šŸŽÆ

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!

What are JavaScript Statements? šŸ“

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.

Variable Declaration šŸ’”

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.

javascript
// 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.

Arithmetic Operations šŸ’”

Now that we have a variable, let's perform some calculations! JavaScript supports the following arithmetic operations:

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulus (%)
  • Increment (++)
  • Decrement (--)
javascript
// 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--; // 5

Comparison Operators šŸ’”

Comparison operators allow us to compare two values and determine their relationship. JavaScript supports the following comparison operators:

  • Equal to (==)
  • Not equal to (!=)
  • Greater than (>)
  • Less than (<)
  • Greater than or equal to (>=)
  • Less than or equal to (<=)
javascript
// 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; // true

Logical Operators šŸ’”

Logical operators allow us to combine multiple conditional statements and make more complex decisions. JavaScript supports the following logical operators:

  • AND (&&)
  • OR (||)
  • NOT (!)
javascript
// 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; // false

Control Flow Statements šŸ’”

Control 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 statements
  • else statements
  • else if statements
  • switch statements
  • Loops (for, while, do-while)

Quiz šŸŽÆ

Quick Quiz
Question 1 of 1

What is the result of the following code?

Stay tuned for our next lesson on JavaScript Control Flow Statements! šŸš€