Statements
In javascript statements are simply the lines of code that's been written into a script and that gets executed by the web browser. It works the same way like in most other programming languages, meaning statements are just different lines of commands and instructions for things you want the computer to do. For example a set of statements for how to make someone make coffee could perhaps look something like this:
- Get the coffee machine
- Fill with water
- Add some coffee
- Turn on the heat
It works the same way in javascript when you're communicating with the web browser and trying to make it do the things you want it to do. Probably not make coffee though. You give it different statements and it will execute them one by one in the script from top to bottom until there are no more left. It will do everything its been instructed like writing out strings, solve math problems with numbers, execute functions, create objects etc. When everything is done the script feels that it's done its job and ends. Well it's perhaps a little more complicated than that, but that'll come later. Below is an example on how the web browser executes three javascript statements after each other.
<body>
<script>
document.write('First this line will be printed out.<br>');
document.write('Then this will be printed.<br>');
document.write('Lastly this.');
</script>
</body>
People mostly write every statement on their own row and end them with a semicolon. It's not required but gets very readable. Something to think about is that javascript actually ignores whitespace except around some reserved words like function, while, var etc, at newlines and in strings. So it doesn't matter if something's written like (1 + 1 = 2) or (1 + 1 = 2). Javascript will read it like (1+1=2).
// Statements can be written like this
a = 3;
b = 4;
c = a + b;
// Or like this
a = 10; b = 14; c = a - b;
// Semicolons are optional
a = 25
b = 5
c = a / b
All statements in javascript must either end with a semicolon or with a new line to be separated. The reason semicolons can be omitted is because javascript has something called "auto semicolon insertion" which automatically adds a semicolon at new lines where there's none (except in some situations). This was really just added to make the language easier for beginners, and can be risky to rely on. It can result in errors that can be very hard to find.
If there's no semicolon or newline between two statements the first one will collide with the second, leading to a syntax error and everything crashes.
Comments in javascript
var x = 5;
// This is a comment in javascript
// Double slash makes a line a comment
var y = x * 2;
/*
Block comments over multiple lines are written with
slash + asterix
*/
People used to html and css have probably found out it's sometimes a good idea to make comments in your code. Comments are just text that the web browser totally ignores and won't show anywhere on a site, except in the source code. So what's that good for then one could ask himself. They are actually very useful and developers put them everywhere in their code as notes that explains what code does. For example what a function does.
It's also very important for teamwork, so when someone else looks at your code they can easily understand what it does. Without any comments code can be really hard to understand, unless you have very clear variable names and function names. Sometimes it just looks like the matrix with variables, functions and letters all over the place.
There's a joke saying the hardest thing in programming is not really coding, but writing comments. Here's a thread with some tips on both good and bad ways of commenting code: https://stackoverflow.com/q/184618.
Try it yourself
- Write some simple javascript statements like for instance document.write(‘Some text'); or document.write(4 + 6);
- Write a couple comments between the statements.