Learn JavaScript Basics by Directing a Construction Site

Master the fundamentals of JavaScript with this interactive tutorial

Get Started

Skip to Practice Section

Nice Work!

JS Construction Site

Give Feedback

1: Variables

7: Comparison

10: If Statements

15: Methods

21: Functions

JavaScript Syntax Comes to Life (0/20)

JavaScript is going to seem like a whole lot of gibberish at first. We are here to get past that phase as quickly as possible.

Why do I need this?

If this is your first coding language, you are going to want to understand all the basic programming principles. This tutorial will use a construction site analogy to show you all the characters that indicate action. So, when you glance at a page of JavaScript, you will be able to trace variables, functions and objects.

What is the focus?

We will cover all the basics, from variables through functions. But, most importantly, when you look at any piece of JavaScript, you will be able to figure out what pieces are being used and why!

What do I need to know first?

All you need to know is why a person needs JavaScript in the first place! No prior coding knowledge required.

Now, imagine you are directing the construction of a new house...

Go to Step 1

Intro to Variables (1/20)

Variables hold values throughout your script.

What can variables do for ME?

Variables allow you to track specific traits and values as you manipulate your website. They link all your functions, event handlers, you name it together. In order to begin to track a specific value, you declare a variable with a statement.

var site ='active';

It's like a truck that carries a value

Think of your entire JavaScript file as a construction site. The variables are trucks that carry values to the appropriate place around the site at any given point. The =, or assignment operator, can change this value.

The = sign is a ramp that loads a value up so the variable can carry it around

Hover over the statement to the right to see how = works.

Intro to Variables (2/20)

Variables hold values throughout your script.

Other Key Concepts Present

There are two key concepts in this statement. One is the semi-colon at the end, which means that the statement has ended. You need to include this every time you declare a new variable. Also, notice the word active in quotes. This is called a string. That means that it is a word, and not a number or a boolean or other.

var site ='active';

The semi-colon is a stake

Stakes signify boundaries. They show when a statement has ended. Anything to the right of the stake might as well not even be in your script. It also prevents one line from mixing with the following line when the code is interpreted by the browser.

Hover over the statement to the right to see how ; works.

Back

Intro to Variables (3/20)

Variables hold values throughout your script.

Your Turn!

You are the director of this construction site. Time to name a foreman.

Create a variable named foreman with a value your choice. The value needs to be a string.

After typing an answer, hover over the statement to the right if you would like a reminder how the pieces work!

Back
Test It

Does your answer contain an '='?

Intro to Variables (4/20)

Variables hold values throughout your script.

Multiple Types of Values

A string is just one type of value that a variable can hold. Here are a few others:

  • Number: Something you can use for math!
  • Boolean: 'True' or 'false', like flipping a swtich
  • Null: Empty value, to be added at a later point
  • Object: Store more complex data, rather than one line at a time

var employees = 20;

Your turn!

Create a variable named days and set it to 100 to show how long you have been on the job.

After you start typing, hover over the statement to the right if you would like a reminder how the pieces work!

Back
Test It

Is your answer a number or string?

Intro to Variables (5/20)

Variables hold values throughout your script.

Multiple Types of Values

A string is just one type of value that a variable can hold. Here are a few others:

  • Number: Something you can use for math!
  • Boolean: True' or false, like flipping a swtich
  • Null: Empty value, to be added at a later point
  • Object: Store more complex data, rather than one line at a time

var dealWon = false;

This is called a boolean. There are two options: true or false. You can use it to flip a switch in your code. So if you want to indicate if something is active or inactive, use this.

Your turn!

Create a variable names siteActive and set it to false to show that nobody is onsite.

After you start typing, hover over the statement to the right if you would like a reminder how the pieces work!

Back
Test It

Is your answer a number or string?

Intro to Variables (6/20)

Variables hold values throughout your script.

Multiple Types of Values

A string is just one type of value that a variable can hold. Here are a few others:

  • Number: Something you can use for math!
  • Boolean: True' or false, like flipping a swtich
  • Null: Empty value, to be added at a later point
  • Object: Store more complex data, rather than one line at a time

employeeCount = 20;

You can also change the value of a variable. Check out the syntax above. Notice how we do not declare "var" again? That means we are modifying the value of a previously created variable.

Your turn!

  1. Create a variable contractors and set it to 5.
  2. You then choose to hire 5 more contractors
  3. So change the value of the variable to 10 in line 2.

After you start typing, hover over the statement to the right if you would like a reminder how the pieces work!

Back
Test It

Is your answer a number or string?

Comparison Logic (7/20)

Learn how to compare two values

An Alternative Kind of Equals Sign

Alright, since the = sign is the assignment operator, we definitely cannot use that to compare two values.

To check if two values are identical, we will use ===, or the operator for strict equality.

var checkins = 20; var checkouts = 20; checkins === checkouts;

This would result in true, because 20 is exactly equal to 20. Notice how this is different from a normal equals sign. You are not assigning any values. You are just checking if two numbers are equal.

Your turn!

Check if the two values are exactly equal using strict equality .

var workers = 20; var hardHats = 19;

Back
Test It

Did you remember to use strict equality?

Comparison Logic (8/20)

Learn how to compare two values

An Alternative Kind of Equals Sign

Since 20 does not equal 19, the last statement would return false.

We can do the same thing with strings. Pretty straightforward- if the strings match exactly, it is true!

var foreman1 = 'Ted'; var foreman2 = 'Ed'; foreman1 === foreman2;

This would result in false, because the strings are not the exact same

Your turn!

Create a statmement that compares roofType with a word that is identical to its value.

var roofType = 'shingles';

Back
Test It

Is your answer a number or string?

Comparison Logic (9/20)

Learn how to compare two values

An Alternative Kind of Equals Sign

Remember the difference between declaring a variable and setting its value at a later point.

Your turn!

Check out the statement below. Complete the last line to make a true statement that compares the value of nailCount.

var nailCount= 500; nailCount = nailCount + 200;

Back
Test It

Is your answer a number or string?

Intro to if Statements (10/20)

Write your code based on the result of a strict equality comparison

If Statement Fundamentals

Since we just learned how to compare variables and values, we now need to learn how to write code that depends upon a comparison statement, or condition .

If statements allow us to run a specific set of instructions based on the result of a comparison statement.

if(contract === 'signed')

The if statement sets up caution cones

We can only enter the if statement if we have passed the condition in between the parentheses

This makes our comparison statement actionable! With an if statement, we have to figure out if the condition is true before we can proceed.

Check out the statement to the right. Hover over the ( or ) and see how it works.

Back

Intro to if Statements (11/20)

Write your code based on the result of a strict equality comparison

If Statement Fundamentals

Think of the if statement and two parentheses as cautionary cones. You can only enter the following statements if the condition between the cones is true.

Write an if statement that looks at a variable siteActive and evaulates whether it is false or not.

After you start typing, check out the right section to see if you are on the right track.

Back
Test It

Did you include a condition?

Intro to if Statements (12/20)

Write your code based on the result of a strict equality comparison

Bringing in the Body

Okay, now we need to outline the code that will run based on whether we can enter the traffic cones. We can do this with brackets {}.

This is very important!

if(siteActive === false){ var gate = 'closed'; }

The {} are like a safety fence

If the cones tell use that we need to pause to see if we are allowed in, the brackets {} tell us what happens next!

These statements will only run if the if condition evaluates to true. In this case, if the site is not active, the gates will be closed.

Check out the statement to the right. Hover over the brackets {} or parentheses () and see how it works.

Back

Intro to if Statements (13/20)

Write your code based on the result of a strict equality comparison

Change a Value

Let's change our code based on an if statement! We are going to declare a value, then change it based on the result of an if statement.

  1. The variable siteStatus has been initiated with a value of 'open'.
  2. Write an if statement that checks if the variable weather is set to 'rainy'
  3. If it is, change the value of siteStatus to 'closed'

var siteStatus = 'open'; var weather = 'rainy'; }

After you start typing, check out the right section to see if you are on the right track.

Back
Test It

Did you include a condition?

Intro to if Statements (14/20)

Write your code based on the result of a strict equality comparison

Else Statements

Let's add in an else statement. It's pretty simple- if the if statement evaluates to false, the else code will run!

  1. The variable bathrooms has been initiated with a value of 1.
  2. Write an if statement that checks if the variable children is set to 2
  3. If the value is not 2, change the value of bathrooms to 2

var bathrooms = 1; bathrooms = 3; } else { }

After you start typing, check out the right section to see if you are on the right track.

Back
Test It

Did you include a condition?

Intro to Member Operator (10/20)

The member operator indicates what elements and methods are a member of what group

Members show grouping

Imagine you are directing your construction site, and you need someone to dig a ditch to put in some plumbing. You see some guy sitting idly, and you yell at him to get on it. Unfortunately for you, that guy was an electrician. And now he is going to cost you thousands of dollars with his mistakes.

The member operator clearly defines what elements are members of which groups, and what those elements are capable of. So if you ordered an electrician to dig a hole, your code can yell at you and tell you that it is not possible.

crane.pickup();

The . indicates an order is being given

The ., or the member operator quickly shows the relationship between these two. Crane, on the left, is an object, which has specific parts and capabilities. On the right, pickup(), is the method() , which is a specific capability of the crane

The . is like you giving orders to a specific object

Check out the statement to the right. Hover over the . and see how it works.

Back

More on Members (11/20)

The member operator indicates what elements and methods are a member of what group

Members show grouping

These are NOT members like members of a club. A better way to describe: what are the capabilities of the group and its members? Let's look at the architecture team for this site. This is how the team is organized, and how you might call people or methods up to do certain things.

architectureTeam.technicalWriter

Check out the hierarchy to the right. Hover over each element to see relationships.

Back

More on Members (12/20)

The member operator indicates what elements and methods are a member of what group

Multiple Members

Check out the following statement with multiple members.

crane.claw.pickup();

  1. We start with the crane object
  2. We give the order to focus on the claw, specifically. Nothing has happened yet.
  3. We order the pickup() method. In this new arrangement of the crane object, only the claw is capable of the pickup() method

Check out the hierarchy to the right. Hover over each element to see relationships.

Back

More on Members (13/20)

Write your own function

Multiple Members

Write your multi member statement. Pick something related to the crane, and a specific action only that person or element could complete.

                        
                    

Back
Test It

Did you include a method?

Intro to Parameters (14/20)

Parameters offer specific instructions for a method to act on.

Parameters give Direction to Functions

Check out the following statement with one parameter.

crane.pickup('wood');

  1. We tell the crane to complete the pickup method
  2. It must pickup wood, specifically.

document.getElementById('wood')

Now this is some real JavaScript! It looks at the document object, and gives it an instruction that only it can understand. In this case, it grabs an element by ID 'wood', which is ready to be acted on.

Check out the statement to the right. Hover over the parameter to see relationships.

Back

Intro to Parameters (15/20)

Write your own function

Grab some Parameters

Write your own crane method with parameters. You want your crane to accelerate() by 5 miles per hour. Remember to use a number, not a string

                        
                    

Back
Test It

Did you include a number?

Intro to Functions (16/20)

Functions are repeatable and reusable code segments

Create your First Function

Now we are getting somewhere! So far, we have covered topics related to objects. Now let's get into functions, which are universal reusable code snippets that become the building blocks of full apps. Check out the function below. Can you guess what it does?

var bricks= 8; function addBrick(count){ return count + 1; } addBrick(bricks);

Can you guess what happened here? Here it is- this function simply adds one to an input. We will cover this whole thing over the next few steps. For now, let's just focus on the addBrick() function definition, lines 2-4. This creates a function that can be used later.

  • The function takes a parameter of count. This value attached to count is now available for use in the function
  • The return statement tells us what the result of the function will be

Wow. That was boring. See the path of count within the function on the right.

Back

Intro to Functions (17/20)

Functions are repeatable and reusable code segments

Track Values Throughout Your Functions

Two new things we are going to cover: The meaning of brackets {}, and how to actually call the function.

var bricks= 8; function addBrick(count){ return count + 1; } addBrick(bricks);

The brackets are kind of like the safety fence that you see on construction sites- they show you an active area where there is ongoing activity. This is where the action takes place.

How To Call A Function

The function is only being defined in lines 2-4. That prepares the sequence of actions for use later in your script. On line 5, we actually call the addBrick() function.

  1. The bricks truck delivers a value of 8 to line 5
  2. The addBrick crane brings the value up to line 2 to be used in the function. It drops it on the count parameter.
  3. The count parameter brings the value of 8 to line 3 to be used in the function

See that sequence by hovering over the lines on the right

Back

Intro to Functions (18/20)

Functions are repeatable and reusable code segments

Track Values Throughout Your Functions

Time for a test! The situation has changed.

  1. Now, you start with a stack of 20 bricks.
  2. And you need to rewrite your function so that it doubles the original amount of bricks. You will need to use the multiply operator.

function addBrick(count){ } addBrick(bricks);

To get it right, consider the following:

  • Which line determines the starting brick amount?
  • Which line determines how we modify the original amount?

See it live on the right

Back
Test It

Did you include a number?

Intro to Functions (19/20)

Functions are repeatable and reusable code segments

Track Values Throughout Your Functions

Okay, so we have done a bunch of math in the past two steps, but we forgot one thing! We need to save the output of the function somewhere!

var bricks= 20; function addBrick(count){ return count * 2; } var finalBrickCount = addBrick(bricks);

When you call addBrick() at line 5, it simply removes the value from the bricks truck (20, in this case)

  1. The value of 20 becomes the parameter in line 2, then...
  2. 20 is fed into the 'return' statement on line 3.
  3. But it needs somewhere to go from there!

We need to look at line 5 again. Our finalBrickCount truck can now load up the result of calling the addBrick() function.

See that sequence by hovering over the lines on the right

Back

Intro to Functions (20/20)

Functions are repeatable and reusable code segments

Write Your Own Function

Check out the code below.

  1. You need to write a function, pourConcrete(), that takes a parameter walkway...
  2. ... and triples the amount of concrete that is poured in. You will need to use the multiply operator.

var concrete= 10; } pourConcrete(concrete)

See it live on the right

Back
Test It

Did you include the correct parameter name?

1

2

3

4

5

6

7

site
'active'

var site = 'active';

This is the variable name. It carries the variable value around.
This is the variable value. It can be easily changed, which you will do frequently.
site
'active'

var site = 'active';

This is the variable name. It carries the variable value around.
This is the variable value. It can be easily changed, but it will always be attached to the name.
This semi-colon ends every statement and sets a boudary for what your browser should read. Semi-colons only end statements, so the browser reads what is to the left of it.
foreman
'yourName'

This is the variable name. It carries the variable value around.
This is the variable value. It can be easily changed, but it will always be attached to the name.
This semi-colon ends every statement and sets a boudary for what your browser should read. Semi-colons only end statements, so the browser reads what is left of it.
Advance to Step 4
days
100

This is the variable name. It carries the variable value around.
This is the variable value. It can be easily changed, but it will always be attached to the name.
This semi-colon ends every statement and sets a boudary for what your browser should read. Semi-colons only end statements, so the browser reads what is left of it.
Advance to Step 4
siteActive
false

This is the variable name. It carries the variable value around.
This is the variable value. It can be easily changed, but it will always be attached to the name. It is a boolean in this case
This semi-colon ends every statement and sets a boudary for what your browser should read. Semi-colons only end statements, so the browser reads what is left of it.
Advance to Step 4
contractors
5

This is the variable name. It carries the variable value around.
This is the variable value. It can be easily changed, but it will always be attached to the name.
This semi-colon ends every statement and sets a boudary for what your browser should read. Semi-colons only end statements, so the browser reads what is left of it.
contractors
10

This is the variable name. It carries the variable value around.
This is the variable value. It can be easily changed, but it will always be attached to the name. It is a boolean in this case
This semi-colon ends every statement and sets a boudary for what your browser should read. Semi-colons only end statements, so the browser reads what is left of it.
Advance to Step 4
Advance to Step 8
Advance to Step 9
Advance to Step 10

if(contract === 'signed')

The if statement will let us enter the following code block based on the result of the condition .
We need to figure out if this condition evaluates to true.

The if statement will let us enter the following code block based on the result of the condition .
We need to figure out if this condition evaluates to true.
Advance to Step 4

if(siteActive === false) {

The if statement will let us enter the following code block based on the result of the condition .
We need to figure out if this condition evaluates to true.
gate
'closed'

var gate = 'closed';

This is the variable name. It carries the variable value around.
This is the variable value. It can be easily changed, but it will always be attached to the name.
This semi-colon ends every statement and sets a boudary for what your browser should read. Semi-colons only end statements, so the browser reads what is to the left of it.

}

siteStatus
'open'

var siteStatus = 'open';

This is the variable name. It carries the variable value around.
This is the variable value. It can be easily changed, but it will always be attached to the name.
This semi-colon ends every statement and sets a boudary for what your browser should read. Semi-colons only end statements, so the browser reads what is to the left of it.

The if statement will let us enter the following code block based on the result of the condition .
We need to figure out if this condition evaluates to true.
siteStatus
'closed'

This is the variable name. It carries the variable value around.
This is the variable value. It can be easily changed, but it will always be attached to the name.
This semi-colon ends every statement and sets a boudary for what your browser should read. Semi-colons only end statements, so the browser reads what is to the left of it.

}

Advance to Step 4
bathrooms
1

var bathrooms = 1;

This is the variable name. It carries the variable value around.
This is the variable value. It can be easily changed, but it will always be attached to the name.
This semi-colon ends every statement and sets a boudary for what your browser should read. Semi-colons only end statements, so the browser reads what is to the left of it.

The if statement will let us enter the following code block based on the result of the condition .
We need to figure out if this condition evaluates to true.
bathrooms
3

bathrooms = 3;

This is the variable name. It carries the variable value around.
This is the variable value. It can be easily changed, but it will always be attached to the name.
This semi-colon ends every statement and sets a boudary for what your browser should read. Semi-colons only end statements, so the browser reads what is to the left of it.

}

else {

bathrooms
3

This is the variable name. It carries the variable value around.
This is the variable value. It can be easily changed, but it will always be attached to the name.
This semi-colon ends every statement and sets a boudary for what your browser should read. Semi-colons only end statements, so the browser reads what is to the left of it.

}

Advance to Step 4

crane.pickup();

The object is to the left of the ., it is the thing that you are ordering around.
The method on the right is a specific capability of the object.

archictectureTeam

.technicalWriter

.adminAssistant

.takeNotes()

.holdWeeklyMeeting()

.draftPlans()

crane

.boom

.claw

.pickup()

.drop()

.reverse()

.rotate()

.accelerate()

.decelerate()

The object is to the left of the ., it is the thing that you are ordering around.
The method on the right is a specific capability of the object.
Advance to Step 4

crane.pickup('wood');

The pickup method would be pretty useless without adding a parameter!
The end result of this statement is that we have wood that we can now put to use in another function.

The accelerate method would be pretty useless without specifiying a speed!
The end result of this statement is that our crane is moving around faster and can now complete other tasks.
Advance to Step 4

function addBrick(count){

function addBrick defines a new function, ready to be called later in your code.
count is an argument, a local variable that carries a value ready to be used in this function

8

return count + 1;

return shows the end result of the function
The count variable receives a value of 8, which was picked up by the crane in the first line

}

Advance to Step 4
bricks
8

var bricks = 8;

Step 1/4 - This is the variable name. It carries the variable value around.
Step 1/4 - This is the variable value. It can be easily changed, which you will do frequently.

8

function addBrick(count){

Step 3/4 -function addBrick defines a new function, which you call at line 5 of your code
Step 3/4 - count is an argument, a local variable that carries a value ready to be used in this function. It is currently 8 based on the bricks function in line 5.

8

return count + 1;

Step 4/4 - return shows the end result of the function
Step 4/4 - The count variable receives a value of 8, which was picked up by the crane on line 2

}

8

addBrick(bricks)

Step 2/4 - addBrick calls the function you defined above
Step 2/4 - The parameter is bricks, so it grabs the value of 8 off the bricks truck.
bricks
20

Step 1/4 - This is the variable name. It carries the variable value around.
Step 1/4 - This is the variable value. It can be easily changed, which you will do frequently.

function addBrick(count){

Step 3/4 -function addBrick defines a new function, which you call at line 5 of your code
Step 3/4 - count is an argument, a local variable that carries a value ready to be used in this function. It is currently 8 based on the bricks funtion in line 8.

20

Step 4/4 - return shows the end result of the function
Step 4/4 - The count variable receives a value of 8, which was picked up by the crane on line 2

}

addBrick(bricks)

Step 2/4 - addBrick calls the function you defined above
Step 2/4 - The parameter is bricks, so it grabs the value of 8 off the bricks truck.
Advance to Step 4
bricks
20

var bricks = 20;

Step 1/5 - This is the variable name. It carries the variable value around.
Step 1/5 - This is the variable value. It can be easily changed, which you will do frequently.

20

function addBrick(count){

Step 3/5 -function addBrick defines a new function, which you call at line 5 of your code
Step 3/5 - count is an argument, a local variable that carries a value ready to be used in this function. It is currently 20 based on the call of the addBricks function in line 5.

20

return count * 2;

Step 4/5 - return shows the end result of the function
Step 4/5 - The count variable receives a value of 20, which was picked up by the crane on line 2

}

20

var finalBrickCount= addBrick(bricks)

Step 2/5 - addBrick calls the function you defined above

Step 5/5 - We load up the result of the addBrick() function into the finalBrickCount truck.

Step 2/5 - The parameter is bricks, so it grabs the value of 8 off the bricks truck.
concrete
10

var concrete = 10;

Step 1/4 - This is the variable name. It carries the variable value around.
Step 1/4 - This is the variable value. It can be easily changed, which you will do frequently.

Step 3/4 -function addConcrete defines a new function, which you call at line 5 of your code
Step 3/4 - walkway is an argument, a local variable that carries a value ready to be used in this function. It is currently 10 based on the bricks funtion in line 5.

10

Step 4/4 - return shows the end result of the function
Step 4/4 - The count variable receives a value of 10, which was picked up by the crane on line 2

}

pourConcrete(concrete)

Step 2/4 - pourConcrete calls the function you defined above
Step 2/4 - The parameter is concrete, so it grabs the value of 10 off the bricks truck.
Done!