Let's get started! This chapter will introduce you to the fundamentals of programming including values, types, and program structure.
Values and types
A value is a piece of information used in a computer program. Values exist in different forms called types. The type of a value determines its role and operations available to it.
A number is a numerical value (thanks Captain Obvious). Let's go beyond that though! Like mathematics, you can use integer values (i.e. whole numbers) such as 0, 1, 2, 3, etc, or real numbers (i.e. with decimals) for greater accuracy.
Numbers are mainly used for counting. The main operations you'll see are summarized in the following table:
8 * 4 and press Enter. You get the expected result:
32. Test other operations!
You can also define strings with a pair of single quotes: 'This is a string.' Whether it's best practice to use single or double quotes is a whole political argument in development. Use whichever you like, but don't mix the two!
To include special characters in a string, use
\ (backslash) before the character you want to add. For example, type
\n to add a new line within a string.
In your browser console, enter the line
"Helloooooo string! \nMore lines here!" (including the quotes). You'll get a two-line result.
You can not add or remove string values like you'd do with numbers. However, the
+ operator can be applied to two string values. This will join two strings together, otherwise called concatenation.
In the browser console, type
"Hey" + " there." You get the result
console.log(). The value you want displayed is enclosed in parentheses and followed by a semicolon. It could, for example, be a number or a string.
Displaying text on the screen (the famous example "Hello world") is often the first thing you'll do when you learn programming. It's the classic example. You've already taken that first step!
We already defined a computer program as a list of commands telling a computer what to do. These orders are written as text files and make up what's called the "source code" of the program. The lines of text in a source code file are called lines of code.
The source code may include empty lines: these will be ignored when the program executes.
You usually write only one statement per line.
When a program is executed, the instructions in it are "read" one after another. It's the combination of these individual results that produces the final result of the program.
Hop back to Sublime and add the contents below to your
course.js file. The instructions in here will be read and executed, line by line.
By default, each line of text in the source files of a program is considered a statement that should be executed. You can prevent certain lines from executing by putting a double slash before them:
//. This turns the code into a comment.
course.js by adding
// on several lines. You'll notice the text grays out:
course.html file in Chrome, and you'll notice that the commented-out lines no longer appear in the console. As we hoped, they weren't executed!
Comments are great for developers so you can write comments to yourself, explanations about your code, and more, without the computer actually executing any of it.
/* A commenton multiplelines */// A comment on one line