Syntax is the set of rules for how words or symbols must be arranged to form a correct sentence or line of code; semantics is about what that arrangement actually means. A line of code can have perfect syntax and still do the wrong thing, because getting the meaning right is a separate matter from getting the structure right.
The core difference
Syntax governs correct structure or form; semantics governs meaning.
- syntax — the rules for how words or code must be arranged to be valid: the code failed because of a simple syntax error, a missing semicolon.
- semantics — the meaning behind language or wording, or the branch of linguistics that studies it: her thesis looked at the semantics of modal verbs in English.
How to tell them apart
Syntax is about form: whether a sentence or a line of code is built correctly, following the grammar or the programming language's rules. Semantics is about content: what that sentence or code actually means or does, regardless of whether it is grammatically tidy.
The mix-up often surfaces in arguments, where semantics is used informally and a little dismissively, as in "that's just semantics", to suggest someone is quibbling over wording rather than the substance of a point. In programming, the two also separate cleanly: a syntax error stops the code from running at all, while a semantic problem is code that runs fine but produces the wrong result.