Syntax
In one line
Syntax is the set of grammar rules that define the correct structure of code in a programming language.
In simple words
Syntax is the grammar of a programming language. Just as English has rules for sentence structure (subject-verb-object), programming languages have rules for code structure — where to put parentheses, semicolons, keywords, and operators.
A syntax error means your code violates these rules — like writing function ( {} instead of function () {}. The computer cannot parse the code, so it refuses to run. Syntax errors are usually caught by the interpreter or compiler before execution.
Each language has its own syntax: Python uses indentation for blocks, JavaScript uses curly braces, Lisp uses parentheses. Learning a language is largely learning its syntax. Good editors help with syntax highlighting, autocomplete, and linting to catch syntax errors as you type.
Example
// Correct syntax
if (x > 0) {
console.log("positive");
}
// Syntax error — missing parenthesis
// if x > 0) {
// console.log("positive");
// }The correct version has matching parentheses and braces. The broken version is missing the opening parenthesis, causing a syntax error.
Common confusions
Confused with: semantics
The difference: Syntax is about structure (is the code well-formed?); semantics is about meaning (does the code do what you intend?). Syntax errors are caught before running; semantic errors (logic bugs) are not.
Related terms
Related Resources
Related Lessons
- How Programs Work
Learn about syntax and semantics.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.