JavaScript basic syntax introduces the core rules and symbols you use every time you write code. Understanding these fundamentals lets you communicate clearly with browsers and avoid common mistakes when building interactive web pages.
Below is a quick reference table that maps key syntax elements, their purpose, and simple examples you can scan while you practice.
| Syntax Element | Purpose | Simple Example | Notes |
|---|---|---|---|
| Variable Declaration (let, const, var) | Creates a named container for data | let age = 25; | Prefer const unless reassignment is needed |
| Strings | Text values enclosed in quotes | const name = "Jane"; | Use single or double quotes consistently |
| Functions | Reusable blocks of logic | function greet() { alert("Hi"); } | Arrow functions (() => {}) are common in modern code |
| Conditionals (if / else) | Branching based on true/false conditions | if (age >= 18) { console.log("Adult"); } | Use === to avoid unexpected type coercion |
| Loops (for, while) | Repeated execution while a condition holds | for (let i = 0; i < 3; i++) { console.log(i); } | Avoid infinite loops by ensuring exit conditions |
Variables and Data Types in JavaScript Basic Syntax
Using let and const
Variables created with let can be reassigned, while const keeps a value fixed after initial assignment. Block scope, defined by curly braces, prevents accidental leaks in functions and loops.
Common Primitive Types
JavaScript basic syntax relies on strings for text, numbers for math, and booleans for logical checks. Use typeof to inspect unknown values during development and testing.
null, undefined, and Symbols
Value null represents intentional emptiness, whereas undefined means a declared variable lacks a value. Symbols create unique identifiers useful for avoiding property name collisions.
Functions and Control Flow
Declaring and Calling Functions
Functions group logic so you can reuse it with different inputs. Define them with the function keyword or concise arrow syntax, and call them by name followed by parentheses.
Conditionals and Comparisons
Conditionals direct program flow with if, else if, and else blocks. Strict equality (===) compares both value and type, reducing bugs from automatic type conversion.
Iteration with Loops
for loops are ideal when you know the iteration count, while while loops suit conditions evaluated at runtime. Always ensure loop variables update to prevent endless repetition.
Objects, Arrays, and Operators
Working with Objects and Arrays
Objects store collections of key-value pairs, enabling structured data models. Arrays hold ordered items and expose powerful methods like map, filter, and reduce.
Operators and Expression Evaluation
Arithmetic, assignment, and logical operators combine values into expressions. Operator precedence and parentheses control evaluation order in complex statements.
Best Practices for JavaScript Basic Syntax
- Always declare variables with const or let to avoid globals
- Prefer strict equality (===) over loose equality (==)
- Use descriptive names for functions and variables
- Keep functions small and focused on a single task
- Format code consistently with a standard style guide
FAQ
Reader questions
What happens if I forget to use const or let when declaring a variable?
Omitting a declaration creates an implicit global, which can overwrite existing variables and cause bugs. Always use const or let to limit scope and keep code predictable.
Can I use == instead of === in JavaScript basic syntax examples?
Using == can lead to surprising coercion results. For reliable comparisons, stick with === so you explicitly check both value and type.
How many spaces should I use for indentation in my code?
Two spaces are common, but teams often standardize on two or four spaces for readability. Consistency matters more than the exact number you choose.
What is the safest way to test a condition that might be undefined?
Check explicitly against undefined or use concise optional chaining when supported. This prevents runtime errors when accessing properties on potentially null values.