JavaScript Basic Syntax: The Core Grammar

Discover the fundamental grammar that powers the web. Learn to use variables, operators, and control flow to bring your code to life.

Lesson ProgressStep 1 of 10
let score = 0;
const name = "Alex";
if (score > 50) { ... }
0 EXP

Welcome! JavaScript is the language that makes web pages interactive. Let's start with the absolute basics.

// Welcome to the JavaScript simulator

Variables & Data Types

Variables are named containers for storing data. In modern JavaScript, you have three keywords to declare them:

  • let: Declares a **block-scoped** variable that **can** be reassigned. This is the most common choice.
  • const: Declares a **block-scoped** constant. It **cannot** be reassigned. Use this when you know the value will never change.
  • var: The old way. It is **function-scoped**, which can lead to unexpected behavior. You should avoid using `var`.

Primitive Data Types

JavaScript is **dynamically typed**, meaning a variable can hold different types of data. The basic types are:

  • String: Text. e.g., "Hello"
  • Number: Both integers and decimals. e.g., 100, 3.14
  • Boolean: Logical values. e.g., true, false
  • null: Represents the intentional absence of any value.
  • undefined: A variable that has been declared but not yet assigned a value.
  • Symbol: A unique and immutable value (used in advanced cases).
  • BigInt: For integers larger than the standard `Number` type can hold.

System Check

Which keyword should you use for a variable that will be reassigned, like a player's score?

Advanced Holo-Simulations

0 EXP

Log in to unlock these advanced training modules and test your skills.


Achievements

🏆
Variable Virtuoso

Correctly declare variables using let, const, and var.

🕵️
Data Type Detective

Correctly identify all primitive JavaScript data types.

🧠
Conditional Commander

Master the use of if/else statements and logical operators.

Mission: Build a Greeting Function

Complete the `greet` function so it takes a `name` and returns the string "Hello, " followed by the name. (e.g., "Hello, Alex"). Our AI assistant will provide real-time feedback.

A.D.A. Feedback:

> System integrity looks stable. Code is valid.

Challenge: Sort the Data Types

Drag each value from the "Values" list into its correct data type box.

Values

"Hello"
undefined
3.14
42
{ a: 1 }
true

string

number

boolean

object

undefined

Challenge: Complete the Syntax

Fill in the missing keywords to make this conditional statement work.

(status "active") {
console.log("Welcome!");
} {
console.log("Please log in.");
}

Consult A.D.A.

Community Holo-Net

Peer Project Review

Submit your "Greeting Function" project for feedback from other Net-Runners.

From Syntax to Interaction: JavaScript and the DOM

Learning JavaScript syntax (variables, loops, functions) is like learning the grammar of a language. It's essential, but it's not the whole story. The *real* power comes when you use that grammar to communicate. In web development, JavaScript communicates with the Document Object Model (DOM).

The DOM is a tree-like representation of your HTML document that the browser creates. JavaScript can read and manipulate this tree, allowing you to change *anything* on your page dynamically—no reloading required.

The 3 Core Steps to Interaction

Almost everything you do to make a page interactive follows this pattern:

  1. Select an Element: You find the HTML element you want to change.
  2. Manipulate the Element: You change its text, style, attributes, or position.
  3. Listen for Events: You wait for the user to do something (like click a button) to trigger your manipulation.

1. Selecting Elements (The "Query")

You use `document` methods to find your HTML elements. The most common is `document.querySelector()`.

// HTML: <h1 id="title">Hello</h1>
// HTML: <button class="btn">Click Me</button>

// JavaScript:
const mainTitle = document.querySelector('#title');
const aButton = document.querySelector('.btn');

2. Manipulating Elements (The "Action")

Once you have an element selected and stored in a variable, you can change its properties.

  • .textContent: Changes the text inside an element.
  • .style: Changes CSS properties (e.g., `.style.color = "red"`).
  • .classList: Adds or removes CSS classes (e.g., `.classList.add("active")`).
// Select the title
const mainTitle = document.querySelector('#title');

// Use a variable to change its text
let username = "Maria";
mainTitle.textContent = `Welcome, ${username}!`;

// Change its style
mainTitle.style.color = "blue";

3. Listening for Events (The "Trigger")

This is where your syntax knowledge comes together. You use `.addEventListener()` to run a function when an event (like a "click") happens.

✔️ Practical Example: A Simple Counter

This code combines everything: variables (`let count`), selection (`.querySelector`), manipulation (`.textContent`), and an event listener.

// HTML:
// <p id="counter-text">Count: 0</p>
// <button id="click-btn">Click Me</button>

// JavaScript:
let count = 0;

const counterText = document.querySelector('#counter-text');
const clickBtn = document.querySelector('#click-btn');

clickBtn.addEventListener('click', function() {
  // Use an operator to increment the variable
  count++;
  
  // Use the variable to update the DOM
  counterText.textContent = `Count: ${count}`;

  // Use control flow to add a style
  if (count >= 5) {
    counterText.style.color = "green";
  }
});
Key Takeaway: JavaScript syntax provides the tools (variables, operators, control flow). The DOM provides the material to work on (the HTML). By combining them with event listeners, you can build fully interactive and modern web applications.

JavaScript Syntax Glossary

Statement
A single instruction or action to be executed. Example: `let x = 5;`
Expression
Any unit of code that resolves to a single value. Example: `5 + 10` (resolves to 15).
Variable
A named container for storing a data value. Declared using `let`, `const`, or `var`.
`let` vs. `const` vs. `var`
`let` and `const` are modern, **block-scoped** keywords. `let` can be reassigned; `const` cannot. `var` is the old, **function-scoped** keyword and is generally avoided.
Primitive Data Type
The most basic data types, which are immutable (cannot be changed). There are 7: `string`, `number`, `boolean`, `null`, `undefined`, `symbol`, and `bigint`.
`string`
A primitive type for text, enclosed in quotes (e.g., `"Hello"`).
`number`
A primitive type for all numeric values, including integers and decimals (e.g., `42`, `3.14`).
`boolean`
A primitive type for logical values: `true` or `false`.
`undefined` vs. `null`
`undefined` means a variable has been declared but not assigned a value. `null` is an intentional assignment of "no value".
Operator
A symbol that performs an operation on one or more values (operands). Examples: `+` (Arithmetic), `===` (Comparison), `&&` (Logical).
`==` vs. `===`
**`==` (Loose Equality):** Compares values after performing type coercion (e.g., `5 == "5"` is true).
**`===` (Strict Equality):** Compares both value and type without coercion (e.g., `5 === "5"` is false). Always prefer `===`.
Truthy & Falsy
In a boolean context (like an `if` statement), all values are either "truthy" (evaluate to true) or "falsy" (evaluate to false).
**Falsy values:** `false`, `0`, `""` (empty string), `null`, `undefined`, `NaN`. Everything else is truthy.
Control Flow
The order in which the computer executes statements, often controlled by `if...else` statements, `switch` cases, and loops.
Loop
A structure that repeats a block of code. Common types include `for`, `while`, `for...of`, and `for...in`.
Function
A reusable block of code designed to perform a specific task. It takes inputs (parameters) and can optionally return a value (output) using the `return` keyword.
DOM (Document Object Model)
The browser's tree-like representation of an HTML document. JavaScript interacts with the DOM to select, create, and manipulate elements on a webpage.

About the Author

Author's Avatar

TodoTutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of web development experts, who have years of experience teaching JavaScript and building robust and accessible web applications.

Verification and Updates

Last reviewed: October 2025.

We strive to keep our content accurate and up-to-date. This tutorial is based on the latest ECMAScript specifications (ES2023+) and is periodically reviewed to reflect industry best practices.

External Resources

Found an error or have a suggestion? Contact us!