Mapagam
  • JavaScript 
  • Web APIs 
  • TypeScript 
  • React 

JavaScript Syntax Explained: Everything You Need to Know

Posted on March 29, 2025 • 5 min read • 1,064 words
Share via
Mapagam
Link copied to clipboard

Master JavaScript syntax with our comprehensive guide. Learn variables, operators, functions, and more to write clean, effective code.

On this page
1. What is JavaScript Syntax? 2. JavaScript Basic Syntax 2.1. Statements 2.2. Variables and Constants 2.3. Data Types 3. Operators in JavaScript 3.1. Arithmetic Operators 3.2. Assignment Operators 3.3. Comparison Operators 3.4. Logical Operators 4. Control Flow Statements 4.1. Conditional Statements 4.2. Loops 5. Functions in JavaScript 5.1. Function Declaration 5.2. Function Expression 5.3. Arrow Functions (ES6) 6. Conclusion

JavaScript is one of the most popular programming languages in the world, primarily used to create interactive effects within web browsers.

1. What is JavaScript Syntax?

JavaScript syntax refers to the set of rules that define how JavaScript programs are written. It consists of keywords, operators, data structures, and more, which allow developers to create meaningful instructions that the JavaScript engine can interpret and execute.

In simple terms, JavaScript syntax is like the grammar of the language. Just as sentences in English follow a set of rules to make sense, JavaScript statements must follow specific patterns to ensure that the code works properly.

2. JavaScript Basic Syntax

2.1. Statements

A statement is a single line of code that performs a specific action, like declaring a variable or calling a function. In JavaScript, statements are usually terminated with a semicolon (;), though it is optional in some cases because JavaScript can automatically insert semicolons where necessary (a feature known as Automatic Semicolon Insertion or ASI).

Example:

let x = 10; // Declaration and initialization of a variable
console.log(x); // Output the value of x to the console

2.2. Variables and Constants

In JavaScript, variables are used to store data values. There are three primary ways to declare variables: var, let, and const.

2.2.1. var

Before ES6 (ECMAScript 2015), var was used to declare variables. It has function scope, meaning the variable is available throughout the function it is declared in, but it can cause issues when used in loops or conditionals due to its scope.

Example:

var x = 10;

2.2.2. let

let is block-scoped, which means it is available only within the block (such as a loop or conditional) in which it is declared. This makes let a safer and more modern choice than var.

Example:

let y = 20;

2.2.3. const

const is used to declare constants—variables that cannot be reassigned after being initialized. Like let, const is block-scoped.

Example:

const pi = 3.14159;

2.3. Data Types

JavaScript has several data types that you can use to store values. These data types can be categorized into primitive types and reference types.

2.3.1. Primitive Data Types

  • String: Used for text. Strings are enclosed in single or double quotes.

    let name = "John";
  • Number: Represents both integer and floating-point numbers.

    let age = 25;
  • Boolean: Represents true or false.

    let isAdult = true;
  • Null: Represents the intentional absence of any object value.

    let x = null;
  • Undefined: Represents a variable that has been declared but not assigned a value.

    let y;
  • Symbol: A unique and immutable primitive value introduced in ES6.

    let sym = Symbol('description');

2.3.2. Reference Data Types

  • Object: Used to store collections of data as key-value pairs.

    let person = {
      name: "Alice",
      age: 30
    };
  • Array: A special type of object used to store ordered collections of data.

    let fruits = ["apple", "banana", "cherry"];
  • Function: A block of code that can be called to perform a specific task.

    function greet() {
      console.log("Hello, World!");
    }

3. Operators in JavaScript

Operators are symbols used to perform operations on variables and values. There are several types of operators in JavaScript.

3.1. Arithmetic Operators

These operators are used to perform mathematical calculations.

  • + (Addition)
  • - (Subtraction)
  • * (Multiplication)
  • / (Division)
  • % (Modulo or remainder)
  • ++ (Increment)
  • -- (Decrement)

Example:

let x = 10;
let y = 5;
console.log(x + y); // 15

3.2. Assignment Operators

These operators are used to assign values to variables.

  • = (Simple assignment)
  • += (Addition assignment)
  • -= (Subtraction assignment)
  • *= (Multiplication assignment)
  • /= (Division assignment)

Example:

let a = 10;
a += 5; // a is now 15

3.3. Comparison Operators

These operators compare two values and return a boolean result (true or false).

  • == (Equal to)
  • === (Strict equal to)
  • != (Not equal to)
  • !== (Strict not equal to)
  • > (Greater than)
  • < (Less than)
  • >= (Greater than or equal to)
  • <= (Less than or equal to)

Example:

let x = 10;
let y = 10;
console.log(x === y); // true

3.4. Logical Operators

These operators are used to combine multiple conditions.

  • && (Logical AND)
  • || (Logical OR)
  • ! (Logical NOT)

Example:

let isAdult = true;
let hasTicket = false;
console.log(isAdult && hasTicket); // false

4. Control Flow Statements

Control flow statements allow the execution of code based on conditions or loops.

4.1. Conditional Statements

Conditional statements allow you to execute code only if certain conditions are met.

4.1.1. if Statement

The if statement is used to execute a block of code if the condition is true.

Example:

if (x > 10) {
  console.log("x is greater than 10");
}

4.1.2. else and else if Statements

The else statement executes a block of code if the condition is false, while else if allows multiple conditions to be tested.

Example:

if (x > 10) {
  console.log("x is greater than 10");
} else if (x < 10) {
  console.log("x is less than 10");
} else {
  console.log("x is equal to 10");
}

4.2. Loops

Loops are used to repeatedly execute a block of code while a condition is true.

4.2.1. for Loop

The for loop is commonly used when you know in advance how many times you want the code to run.

Example:

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

4.2.2. while Loop

The while loop runs as long as the specified condition is true.

Example:

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

4.2.3. do...while Loop

The do...while loop executes the code block once before checking the condition, ensuring that the code runs at least once.

Example:

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

5. Functions in JavaScript

Functions allow you to group related code into a reusable block. You can define functions in JavaScript using the function keyword.

5.1. Function Declaration

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

5.2. Function Expression

Functions can also be assigned to variables.

const add = function(a, b) {
  return a + b;
};
console.log(add(5, 3));

5.3. Arrow Functions (ES6)

Arrow functions are a more concise way to write functions in JavaScript.

const subtract = (a, b) => a - b;
console.log(subtract(10, 5));

6. Conclusion

Understanding JavaScript syntax is crucial for writing effective and maintainable code. In this article, most fundamental concepts covered like basic syntax, variables, operators, control flow, and functions, which are the building blocks of JavaScript programming.

JavaScript Syntax   Learn JavaScript   JavaScript Tutorial   JavaScript Basics   JavaScript Functions  
JavaScript Syntax   Learn JavaScript   JavaScript Tutorial   JavaScript Basics   JavaScript Functions  
 What Are Variables in JavaScript? A Simple Explanation
How to Run JavaScript Code: A Step-by-Step Beginner’s Guide 

More Reading!

  1. Beginner’s Guide to JavaScript Functions (With Best Practices)
  2. How to Use the Geolocation API in JavaScript (With Live Demo)
  3. Beginner’s Guide to the JavaScript DOM API (With Practical Examples)
  4. JSX in React: The Syntax Behind the Magic (With Real-World Examples)
  5. JavaScript Functions Made Easy: Syntax, Parameters & Return Values
On this page:
1. What is JavaScript Syntax? 2. JavaScript Basic Syntax 2.1. Statements 2.2. Variables and Constants 2.3. Data Types 3. Operators in JavaScript 3.1. Arithmetic Operators 3.2. Assignment Operators 3.3. Comparison Operators 3.4. Logical Operators 4. Control Flow Statements 4.1. Conditional Statements 4.2. Loops 5. Functions in JavaScript 5.1. Function Declaration 5.2. Function Expression 5.3. Arrow Functions (ES6) 6. Conclusion
Follow me

I work on everything coding and technology

   
Mapagam
Mapagam is your go-to resource for all things related to frontend development. From the latest frameworks and libraries to tips, tutorials, and best practices, we dive deep into the ever-evolving world of web technologies.
Licensed under Creative Commons (CC BY-NC-SA 4.0).
 
Frontend
JavaScript 
Web Api 
TypeScript 
React 
Social
Linkedin 
Github 
Mapagam
Code copied to clipboard