A programmer writes programs, just as an author writes a book.
A program is just a set of instructions that a computer can read and use to perform a task. Each individual instruction is a line of code known as a statement, which is similar to a sentence in a book. While a sentence in English ends with a period (
;). Syntax refers to the symbols and rules that define the structure of the language, similar to grammar and punctuation, and the semicolon (
A comment is a human-readable note written in the code.
Comments are written in plain English with the purpose of explaining the code. Although comments don’t technically perform any function in the program, it’s crucial to get into the habit of proper documentation to help you or future collaborators understand the intent of your code.
//followed by the comment.
// This is a single line comment.
*/and can span many lines.
A variable is a container that stores data values.
You know a variable as something that can change. In basic algebra, it’s a letter that represents a number. x is a common variable name, but it can just as easily be represented by y, z, or another name.
x has no value or meaning. But you can apply a value to it. For example:
x = 5
x represents 5. You can think of
x as a container that’s storing 5, which is a number.
const keywords. For a comparison of the differences between the two, refer to this article for an explanation.
let x = 5;
This means that the variable
x contains the numeric value of
x, assigns the number data type,
5, with a single equals sign,
=, and the statement ends with a semi-colon,
Variables only need to be declared with
const the first time they’re used. And as you’ve already learned, a variable’s value can change.
Here is an example of a variable with a different data type:
let greeting = "Oh hi, Mark!";
Now the greeting variable contains the string “Oh hi, Mark!”.
A few important things to know about variables:
Tip: Although a variable can have any name, it’s important to choose names that are descriptive yet concise.
Now that we know about variables, we can learn about the types of data that a variable can hold.
A datatype is a classification of data. Programming languages need to have different datatypes to interact properly with values. You can do math with a number, but not with a sentence. So the computer classifies them differently. There are six (6) primitive, or basic, datatypes: strings, numbers, Boolean, null, undefined, and Symbol (new in ES6). Primitives can only hold a single value. Anything that is not one of these primitives is an Object. Objects can contain multiple values.
Here is a demo of these methods. An
alert is a popup window and
alert() and instead use
console.log(). This is because every time
alert() is called, a popup window appears in your browser, so it gets very annoying after a while.
You can always find out the type of a variable by using
typeof. For example: