JavaScript Statements
Now that we know what the main parts of a computer are and that everything to a computer is really about numbers in some way, let’s get into programming.
A program is simply a set of instructions for a computer. You can write a program in many different languages, but we will focus on JavaScript because it is widely available, taught broadly in schools and often used by professionals.
Just a quick note: programming languages can be written in lower-level way (closer to machine code) with a lot of 0s and 1s…or in a higher-level way (which is easier for us to read) with words and symbols that people can easily understand. JavaScript, Java, C++, Python, etc. are all higher-level programming languages; we will stick with higher-level languages in these examples, but you can certainly study lower-level languages, such as Assembly, on your own.
What is a Statement in Programming?
So, if a program is like a book of instructions to a computer, a statement is like a sentence.
Think of a statement in terms of grammar…the programming language you use has certain rules, commonly called syntax, that must be followed in order for the computer to understand your instructions. A sentence, or a statement, can be complex or simple…it just must be syntactically correct in order to be followed.
In JavaScript, a statement can be written in the ways these two statements are written below:
let name = “Danita";
document.write(name);
In JavaScript, a semicolon does not have to be used at the end of a single line, but think of the semicolon like a period—it tells the reader, and the program, when a statement has ended. You only need to use a semicolon when you want to separate multiple statements that are on a single line, in JavaScript, but you may want to use it for ease of reading and habit, because many other languages do require semicolons at the end of their statements.
Also, JavaScript is case sensitive. If I wrote:
let name = “Danita”;
let Name = “Danita;
Those two statement would not be the same in JavaScript. So, be careful when you write your code and name your variable and functions, etc. sometimes there may be a one-letter difference that’s causing you an error.
Finally, JavaScript ignores additional whitespace in your program. If I wrote:
let name = “Danita”;
JavaScript would treat this statement as if the extra whitespace was not there. This, in general, allows you to write other statements in ways that allow you to break up your code and make it more readable and easy to understand.
So, a statement is a syntactically correct instruction to a computer. A statement can be a single line or a complex set of logic that can span several lines, if needed. Regarding the statements above:
let name = “Danita";
document.write(name);
The first statement assigns and initializes a variable called, name, and stores the value Danita in that name variable.
The second line now tells the compute to write the value of the variable name, into the document that is being used. That would result in printing out to the screen, simply:
Danita
So, let’s move on to naming variables as the rules that we follow here will also apply to the naming of functions, objects, etc.