Syntax vs. Semantics in Programming

Brian Cheung
Star Gazers
Published in
3 min readApr 5, 2021

--

Syntax vs. Semantics

On your journey to becoming a software engineer, you will most likely be learning more than one programming language. With different languages, you may have heard the terms “syntax” and “semantics” thrown around here and there, but what do they really mean and why is it important to the context of programming languages? Lets take a closer look.

Syntax

According to Merriam-Webster, the definition of syntax is “the way in which linguistic elements (such as words) are put together to form constituents (such as phrases or clauses)”. Syntax is heavily related to grammar, and usually deals with the order of words when it comes to making a sentence. For example, in English, the sentence

“I like pizza very much”

is syntactically correct; the words follows the basic subject-verb-object structure, and just sounds correct. On the contrary, the sentence

“I like very much pizza”

is syntactically incorrect. Once you get fluent at a language, you can just tell that a sentence isn’t grammatically correct just by reading it. The same concept applies to programming languages. Syntax, in the context of programming, refers to the order and combination of words and symbols to create a correctly structured expression or statement. Just like the hundreds of different speaking languages in the world, syntax can heavily vary between different programming languages. For example, in JavaScript, to make a function that says “hello world” you would write:

function sayHi() {
console.log(“Hello world!”);
}

In Ruby, you would write:

def say_hi()
puts “Hello world!”
end

If you get the syntax wrong, compilers are great at letting you know where you messed up. They’ll usually let you know that you’re missing a closing bracket, parentheses, or have a symbol at the wrong spot.

Semantics

According to Merriam-Webster, the definition for semantics is “the historical and psychological study and the classification of changes in the signification of words or forms viewed as factors in linguistic development”. Or to put it in layman’s terms, the meaning of a sentence. In programming, semantics can help someone understand what a program is doing. For example, in Python, if you read the following:

for i in range(0, 10):
print(i)

you can tell that the program will print out the numbers 0–9.

Tying it all together

So how are syntax and semantics related to each other in the context in programming, and why is this all important? Syntax defines the rules of a programming language, but semantics defines the meaning of the different combination of words and symbols. When a complier reads a syntactically valid block of code, the language’s semantics then determines what it should do with that code. If there is a syntax error, the complier won’t be able to properly read the code. Therefore, you would think to say that all valid code will create a program that has a valid meaning. However, it is possible to have something that is syntactically correct but doesn’t have a valid meaning. For example, in English, the sentence

“John is a married bachelor”

is a syntactically correct sentence. However, the semantics of the sentence express something that cannot be true — a bachelor is a man who is not and has never been married. The same applies in programming. Consider the following JavaScript code:

let x;console.log(x + 3);

While the code follows the proper syntax, what the program is doing doesn’t make sense. It is trying to add the number 3 to a variable that doesn’t have a value. Semantic errors like these usually result in unwanted behavior, and can sometimes be harder to spot because some compilers do not catch these.

I find syntax and semantics particularly important because some people may find learning a completely new programming language daunting. However, it is important to note that after you have learned one language, applying what you’ve learned from that language to a new one isn’t that hard at all. All you are doing is learning new syntax, the semantics of the different languages tend to be similar. You will still be learning how to write functions, for loops, and so on in many different languages, its just that the order of the keywords and symbols may be different.

I hope this article helps new programmers understand the difference between syntax and semantics, and why it is important in coding!

--

--

Brian Cheung
Star Gazers

Software engineering graduate of Flatiron School. Loves all things gaming, photography, and anime.