Syntax, semantics, and pragmatics.
Mastering JavaScript Series
When it comes to languages, both natural and computer languages, syntax, semantics, and pragmatics are used to categorize the descriptions of language characteristics. If you know enough characteristics of a language you are fluent in that language.
These words sound scarier then they actually are. A good way to understand all three is to look at an example in English. In English, syntax refers to the way words and sentences are placed. Usually in English, sentence syntax should follow subject-verb-object agreement.
John prepared the dishes.
Jane washed the dishes.
After the proper syntax is produced based on the pre-determined rules, then it is time to understand the meaning, which in fancy terms we call it the semantics. In the sentences above the meanings are pretty simple. One person cooks the other person washes the dishes. But, watch how the meanings change dramatically once we add some context:
Context 1: All the workers in the restaurant performed their duties on May 20th.
John prepared the dishes.
Jane washed the dishes.Context 2: The couple prepared diner on May 21st 2009.
John prepared the dishes.
Jane washed the dishes.
The fancy term for this context thingie is pragmatics of the language. So for English language to work, all syntax, semantics, and pragmatics are required to convey the full message.
Programming languages are designed in a similar way. Not sure if this was the only way to do it or if we designed it so because we were readily familiar with it.
Now lets see what these words mean in computer programming languages:
Syntax
Is the set of rules that defines the combinations of symbols that are considered to be valid in a given language. It is about the structure or grammar of the language. Some JavaScript syntax rules are:
- if you put
var
before a word that word becomes a variable - the word that you put after
var
shouldn't be one of JavaScript’s reserved words - you can assign values with a single equal sign
=
- you can subtract using the
-
sign
Semantics
Semantics is about the meaning of the sentence constructed by putting together some valid syntax. +
is valid syntax for plus operator but if you type x+1
in a javascript program and run it, it will give you an error. Because you are trying to add 1
to a variable that doesn't exist. Yes, x
is valid syntax, yes, +
is valid syntax, and yes 1
is valid syntax. But it doesn’t mean anything here. Instead something like this could mean something:
Here, you are saying that x
is a variable with a value of 1
and you are adding 1
to a it, and it will give you back 2
Pragmatics
In programing languages pragmatics are the ways in which the language’s features are used to build effective programs. And in JavaScript this is where you will find yourself diving deep into the core concepts of programing and JavaScript. Some examples are:
- what is a variable scope and how can you use it in your programs
- what are closures and how can you use it in your programs
- when do you use arrow functions and when do you use classic functions