JavaScript Syntax

JavaScript syntax is like a set of rules that dictate how you should write JavaScript code so that computers can understand and execute it correctly. Let me give you a quick overview!

1. Variables

  • You use var, let, or const to declare variables. For example:
javascript
  var x = 5;
  let y = 10;
  const z = 15;

2. Data Types

  • JavaScript has various data types, such as numbers, strings, booleans, objects, arrays, etc.
javascript
  var num = 42;
  var text = "Hello, World!";
  var isTrue = true;

3. Operators

  • You can perform operations using operators like +, -, *, /, etc.
javascript
  var sum = 2 + 3;
  var product = 4 * 5;

4. Functions

  • You define functions using the function keyword.
javascript
  function greet(name) {
      return "Hello, " + name + "!";
  }

5. Control Flow

  • You use if, else if, and else for conditional statements.
javascript
  if (x > 0) {
      console.log("Positive number");
  } else if (x < 0) {
      console.log("Negative number");
  } else {
      console.log("Zero");
  }

6. Loops

  • For and while loops are used for repetitive tasks.
javascript
  for (var i = 0; i < 5; i++) {
      console.log(i);
  }

  var counter = 0;
  while (counter < 3) {
     console.log("Counting: " + counter);
     counter++;
  }

7. Objects

  • Objects are collections of key-value pairs.
javascript
  var person = {
      name: "John",
      age: 30,
      isStudent: false
  };

8. Arrays

  • Arrays store multiple values in a single variable.
javascript
  var fruits = ["apple", "banana", "orange"];