• 10 hours
  • Easy

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 4/26/19

1, 2, 3: code!

Log in or subscribe for free to enjoy all this course has to offer!

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.

Every computer language has its own types and values. Let's look at two main types available in JavaScript.

Number

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:

Operator

Job

+

Addition

-

Subtraction

*

Multiplication

/

Division

Most browser consoles allow you to type JavaScript code and execute it. In the Chrome console, simply type8 * 4 and press Enter. You get the expected result:32. Test other operations!

Numbers in JavaScript
Numbers in JavaScript

Strings

A string in JavaScript is a set of characters surrounded by quotation marks, such as "This is a string."

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.

Strings in JavaScript
Strings in JavaScript

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 "Hey there."

String concatenation
String concatenation

Displaying values

When you want to display a value from a JavaScript program, use the JavaScript commandconsole.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.

console.log ( "Hello in JavaScript!");

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!

Program structure

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.

Instructions

Each instruction inside in a program is called a statement. A statement in JavaScript ends with a semicolon, and your program will be made up of a series of these statements.

You usually write only one statement per line.

Execution flow

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.

console.log("Hello in JavaScript!");
console.log("Let's do some math.");
console.log(4 + 7);
console.log(12 / 0);
console.log("Goodbye.");

Comments

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.

Modify course.js  by adding//  on several lines. You'll notice the text grays out:

console.log("Hello in JavaScript!");
// console.log("Let's do some math.");
console.log(4 + 7);
// console.log(12 / 0);
console.log("Goodbye.");

Re-open your 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 comment
on multiple
lines */
 
// A comment on one line

Example of certificate of achievement
Example of certificate of achievement