Introduction to JavaScript Syntax

Variables

Variables are named storage for data. Use var, let, or const to declare variables.

let message = "Hello, World!";
const PI = 3.14;

let declares a block-scoped variable, optionally initializing it to a value.
const declares a block-scoped constant.
var declares a function-scoped or globally-scoped variable.

Data Types

JavaScript dynamically types its variables; it can hold different data types: String, Number, Boolean, Null, Undefined, Object, and Symbol.

let name = "John"; // String
let age = 25; // Number
let isStudent = false; // Boolean
let address = null; // Null
let job; // Undefined

Operators

JavaScript supports arithmetic (+, -, *, /), comparison (<, >, ==, !=), logical (&&, ||, !), and assignment (=, +=, -=) operators.

let sum = 10 + 5; // 15
let isEqual = sum === 15; // true
let isAdult = age >= 18; // true

Control Structures

Control structures direct the flow of your code with conditionals and loops.

// If statement
if (isAdult) {
  console.log("Is an adult.");
}

// For loop
for (let i = 0; i < 5; i++) {
  console.log(i);
}

// While loop
let i = 0;
while (i < 5) {
  console.log(i);
  i++;
}

Functions

Functions are blocks of code designed to perform a particular task, defined using the function keyword.

function greet(name) {
  return "Hello, " + name + "!";
}
console.log(greet("Alice"));

Objects

Objects are collections of properties. A property is an association between a key and a value.

let person = {
  name: "John",
  age: 30,
  greet: function() {
    console.log("Hello, " + this.name);
  }
};

person.greet(); // Outputs: Hello, John

This guide covers the fundamentals of JavaScript syntax, including variables, data types, operators, control structures, functions, and objects. Understanding these basics is crucial for developing applications in JavaScript.