Getting Started

Table of Content

Table of Content

Table of Content

Syntax and Structure

JavaScript syntax defines the set of rules for writing valid JavaScript code. It consists of statements, expressions, operators, and control structures that determine how the script is executed.

JavaScript Statements

A JavaScript program consists of a sequence of statements. Each statement performs an action and is typically written on a new line.

Example:

console.log("Hello, World!"); // Outputs text to the console
let x = 10; // Variable declaration
x += 5; // Arithmetic operation

JavaScript statements can be terminated with a semicolon (;), though it is optional in most cases.

Comments in JavaScript

Comments are used to document code and prevent execution of specific lines.

Single-line comment:

Multi-line comment:


Variables and Constants

Variables store data values and can be declared using var, let, or const.

Declaring Variables

let name = "John"; // String variable
var age = 25; // Number variable
const PI = 3.14; // Constant (cannot be reassigned)
  • let – Block-scoped variable (recommended for most use cases).

  • var – Function-scoped variable (older syntax, avoid using it in modern development).

  • const – Block-scoped, immutable variable (cannot be reassigned).

Variable Naming Rules

  • Must begin with a letter, underscore (_), or dollar sign ($).

  • Cannot use reserved JavaScript keywords.

  • Case-sensitive (myVariable and MyVariable are different).

Data Types in JavaScript

JavaScript is dynamically typed, meaning variables do not have fixed types.

Primitive Data Types

JavaScript has six primitive types:

  • String"Hello"

  • Number42, 3.14

  • Booleantrue, false

  • Undefined – A variable that has been declared but not assigned a value

  • Null – Represents an intentional absence of value

  • Symbol – Unique and immutable identifier (used in advanced programming)

Example:

let text = "JavaScript"; // String
let count = 100; // Number
let isAvailable = true; // Boolean
let value; // Undefined
let empty = null; // Null

Non-Primitive (Reference) Data Types

  • Objects – Collections of key-value pairs.

  • Arrays – Ordered lists of values.

  • Functions – Blocks of reusable code.

Example:

let person = { name: "Alice", age: 30 }; // Object
let numbers = [1, 2, 3, 4, 5]; // Array
function greet() { console.log("Hello!"); } // Function

Operators in JavaScript

JavaScript includes several types of operators:

Arithmetic Operators

Used for mathematical operations.

let a = 10, b = 5;
console.log(a + b); // Addition (15)
cnsole.log(a - b); // Subtraction (5)
console.log(a * b); // Multiplication (50)
console.log(a / b); // Division (2)
console.log(a % b); // Modulus (0)

Comparison Operators

Used for value comparisons.

console.log(10 > 5); // true
console.log(10 < 5); // false
c0nsole.log(10 == "10"); // true (loose equality)
console.log(10 === "10"); // false (strict equality)
console.log(10 !== 5); // true (not equal)

Logical Operators

Used to combine multiple conditions.

console.log(true && false); // false (AND)
console.log(true || false); // true (OR)
console.log(!true); // false (NOT)

Conclusion

This page introduced JavaScript syntax, variables, data types, and operators. In the next section, we will explore control flow structures like conditionals and loops to build more complex logic.

Get Template for free

Get Template for free

Get Template for free