What is a programming language?
In their essence all programming languages are just different ways of communicating with the computer. For those new to programming, they can just look at programming languages as languages corresponding to the spoken languages but with the special purpose of allowing humans to communicate with the computer, all at their core, based on the same concepts as others. Since writing in machine code (1’s & 0’s) isn’t the most convenient way to write instructions for the computer, programming languages provide a way for the developers to not only write code in a language more familiar to them but also allow other programmers to easily comprehend their work.
Different Engines have different “code names”. For example:
* V8 in Chrome and Opera.
* Spider-Monkey in Firefox.
These terms are good to remember because they’re often used in developer articles on internet. For instance, if a feature X is supported by V8, then it probably works in Chrome and Opera.
How do Engines Work?
Engines are pretty complicated. But the basics are easy:
1. The engine reads (“parses”) the script.
2. Then it converts (“compiles”) the script to the machine language.
3. And then the machine code instructs the computer for what’s and what-not’s.
Now that we’re done with the basics of what’s a programming language, it’s time to dive into and start off with our first program:
Here you can start writing code for now.
Now to stop rambling, we’ll just dive into out first line of code: Have the browser pop up with a “Hello, World!” message.
To do that all you would want to do is to write:
and press enter, and voila!
For those working on a terminal, you might want to try the function:
alert(); is a browser specific function.
Let us understand the statements above first before proceeding. The semi-colon at the end of a statement signifies an end of the statement. You’ll notice that your code runs just fine without a semi-colon as the Engine adds it for you but it’s considered good practice to append one at the end of each statement yourself to avoid some situations that you might run into in the future where the Engine does not implicitly add a semi-colon. The circular braces “()” signifies that a function is being called, in this case “alert” which is a predefined function but more on that in later chapters; and in the quotes we write the message that we want to have the browser pop up for us. Contrary to alert, console.log simply logs the message in the console.
Now back to the code, you can write as many statements in a line as you want, each statement separated by a semi-colon:
Hence this would throw an error:
However, there are cases where a line-break does not mean a semi-colon:
Comments form an integral part of a programming language as not only do they help other programmers quickly grasp what a block of code, written by some other developer, does but they also help you get going real quick when you revisit your own code after a while.
// This is a comment spanning over a single line.
alert("Hello, World!"); // This comment follows the statement
Everything following the slashes would be ignored by the engine while parsing the code. Consequently, you cannot continue the code on the same line as you’ve written a comment on:
alert(“Hello, World!”); // Following code will be ignored alert(“Hello again!”);
Sometimes you might want to write a comment that spans mulitple lines, this can be done by adding two asterisks in between the 2 forward slashes like this:
/* This comment
Why you would want to nest a comment is anyone’s guess but I guess it’s worth mentioning that it is not supported, such a code will die of an error:
A nested comment that would kill the code’s execution with an error
Hopefully this was a good start to your programming journey. In the subsequent chapters we’ll focus on variables and other concepts that make a programming language what it is. To finish off here’s a comment that actually describes the code it’s used for:
// Outputs Hello World
// logs the message on console.