HTML 5000000
HTML 5000000
home page) of a site, the URL does not need a file path after the domain
name—so you will just see a single slash at the end of the address:
http://en.wikipedia.org/
http://en.wikipedia.org
In contrast, if we look at the URL for a file on your local system, we must give
the file path—in fact, most of the URL is the file path:
file:///Users/Kelly/notes.html
If we took away the file path from the above, we would just have:
file://
Images significantly enhance the web's appeal, featuring everything from cat pictures to
maps and advertisements. The img element in HTML, equipped with src (source URL)
and alt (text description) attributes enable the inclusion of these visuals in web pages.
The alt attribute is crucial for accessibility, describing when images fail to load due to
network issues, are missing, or when assisting visually impaired users with screen
readers.
The img element points the browser to an image's location without embedding the file
into the HTML document, functioning as an empty element with no closing tag. It
integrates directly into the text, influencing layout based on its size. This setup
underscores the importance of thoughtfully using images in web design to ensure
aesthetic appeal and accessibility.
Now you've learned about many more HTML elements! This page is a
summary of all the elements you've seen so far. You can use this page as a
reference if you forget how to use one of these elements in your code.
You can also download a PDF version of this page here(opens in a new tab) .
Block elements
Block elements are used for large sections of text, such as paragraphs,
headlines, or lists; and also for some other features such as video players and
tables.
Most block elements have a particular way they are displayed by default:
paragraphs have margins around them; lists have bullet-points or numbered
items; headlines are printed in large text. There is also a generic block
element, div , which has no special defaults.
You will see the div element much more in the next lesson. Because they
don't have any default display settings, div s are heavily used with custom
styling with CSS.
Inline elements
Inline elements do not create a full-width box on the display. They modify the
display of text, or insert other things into the text — such as line breaks,
images, or hyperlinks.
mark — Highlighting. Not very often used, but it's kind of cool.
Some of the inline elements you've seen require attributes, extra information
besides the name of the element itself. Attributes are written inside the
opening tag of the element.
img — Images. Needs a src attribute with a URL, and an alt attribute with
descriptive text.
a — Hyperlinks. Needs an href attribute with a URL.
Images
The syntax for the img tag is like this:
<img src="Image URL here" alt="A description of the
image">
The alt text is used if the image can't be loaded, or if the user can't see
images — such as if the user is using a screen reader(opens in a new tab) .
Links
Hyperlinks allow the user to navigate from one page to another. They are
created using the a element. The destination of the link is written in an
attribute called href ; the link text appears as the contents of the a element.
Here's an example:
<a href="https://en.wikipedia.org/wiki/Hypertext"
target="_blank">
</a>
We suggest doing that in this exercise, or else the link will open inside the
workspace itself when you click on it.
For the images, you can
use https://placebear.com/150/150 and https://placecats.com/150/150 .
will open in a new browser tab. If you want to do this in your own links, you can add the
attribute target="_blank" to the a element. Like this:
We suggest doing that in this exercise, or else the link will open inside the workspace
itself when you click on it.
Start Workspace
Workspaces will shut down after 30 minutes of inactivity. Any running processes will
be stopped.
Start Workspace
## ⚠️Spoiler alert!
Our solution is below. You'll learn more effectively if you give the exercise a strong
effort before comparing your code with ours!
----
Solution
Here's our code, if you want to compare:
<p>
My favorite things:
</p>
<ol>
<li><a href="https://en.wikipedia.org/wiki/Bear"
target="_blank">Bears</a>
<li><a href="https://en.wikipedia.org/wiki/Kitten"
target="_blank">Kittens</a>
</ol>
<img src="https://placebear.com/150/150" alt="A picture of a bear.">
<img src="https://placecats.com/150/150" alt="A picture of a kitten.">
The grammar of HTML does not require that you literally write
a <head> or <body> tag in your HTML code. Many web developers do write
these. However, if you don't, the browser will attempt to place them into your
code itself.
It needs to put the head element around certain elements that belong there,
such as title ; and to put the body element around the elements that form
the document's body. This means that all the head elements must appear
first, and the body elements after.
You can't have a head element, such as title , in the middle of your
document:
<h1>Here is a 😨 problem:</h1>
So even if you choose not to literally write the <head> and <body> tags in your
document, the head and body elements are still created by the browser; and
the rest of the document needs to be consistent with this. But you must make
sure that the title and other head parts appear before any paragraphs, lists,
images, or other body parts.
You might see older HTML documentation that says these tags are required. This was
true in XHTML, an older version of HTML. Today's browsers use HTML5, which does
not require them.
In the next lesson, you will learn much more about how the browser
understands and displays web pages, as you learn about Cascading Style
Sheets.
Task List
Now that you know about the <html>, <head>, and <body> elements, add them to
your notes file.
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
</body>
</html>
You may also want to describe these elements a little. What are some examples of
things that you might put in the <head>? How about the <body>? And what would go
inside the <html> element?
When you're first learning HTML, some of the terms can easily get scrambled in your
head. In particular, it's not uncommon to start mixing up terms like heading, head, title,
and file name. So let's get some practice sorting out what is what.
For the next exercise, have a look at this notes page that is loaded up in a browser
window:
Try the validator!
Take a look at the following HTML. How many error messages do you think
the validator will give for it?
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<img src="wonderful.png">
</body>
</html>
Copy and paste it into the "Direct Input" tab of the validator
at https://validator.w3.org/(opens in a new tab) .
Validating HTML
How many errors do you get? Type the number of errors below.
Note: The validator may also give you warnings as well as errors. This question is just
asking about errors (don't count the warnings in your answer).
For instance, if you write an img element with no alt attribute, a typical browser will
display it just fine. But the language requires the alt attribute to provide a textual
description of an image; this helps search engines and also users with visual
disabilities. By reporting a missing alt attribute as an error, the validator makes sure
that valid HTML will be more accessible.
When you do see an error that you're not sure about, here are a few things you can try:
Copy and paste the error into your favorite search engine.
Search Knowledge(opens in a new tab) to see if someone has previously
encountered this error. If they haven't, you can post a question about it
yourself!
Share the error with others (such as other students or a mentor).
The Web. We talked about what the Web is and—at a basic level—how it
works.
Tools for Editing. You practiced editing web pages using a text editor and
testing your work in a browser.
HTML. You learned the fundamentals of HTML, the computer language we
use to structure and write webpages.
URLs. You learned about URLs, the addresses that we use to refer to
documents and sites on the web.
When you click the Next button, you'll see a pop-up that will ask you to rate
the lesson. After you select a rating, you'll also see a box where you can write
any comments you want to share with us. We love to hear from you!
I can't tell you how much I enjoy reading your comments—so please, keep
'em coming!
Happy learning!
– Abe 😁
In this lesson you're going to learn how to add style to your web pages using
the Cascading Style Sheets (CSS) language. We'll learn about a whole
bunch of key concepts in this lesson, including:
Tree structures. It's not immediately obvious, but the HTML code for your
web page has a branching hierarchy or tree structure. Understanding this
structure will help you apply style to your page more effectively and efficiently.
Using CSS and HTML together. HTML and CSS are different languages, but
we'll learn how to use them together. We'll actually learn three different ways
to integrate CSS with the HTML of your web page.
But also separating them. Although they work closely together, we'll also
learn how to separate our CSS, which defines the style of the page, from the
HTML, which defines the structure. This separation of concerns will make our
web development far more efficient.
A whole lot of style properties. We'll learn how improve the look of our
HTML pages with CSS style properties—from changing the color of your text
to completely changing the layout.
This lesson is definitely more challenging than the last, but we hope you'll
stick with it because the payoff is well worth the work! You'll be tackling some
pretty sophisticated concepts that will dramatically improve your coding
abilities. Not only will you learn far more powerful ways to improve your web
pages, but at the same time you'll also be stretching your knowledge of
computer language syntax. You'll get a better understanding of how computer
languages are put together, and that will serve you well throughout your
journey as a developer.
How to Access Developer Tools in Your browser
The browser shown in the video is Google Chrome, which is what we recommend—but
most modern browsers have developer tools that work similarly. Here are instructions
for a few different browsers:
Google Chrome
Open the Chrome menu at the top right of the browser window (the three
vertical dots) and select Tools > Developer Tools.
You can also simply right-click on any page element and select Inspect.
Safari
1. From the menu bar, select Safari > Preferences, and click Advanced.
2. At the bottom of the box, check the "Show Develop menu in menu
bar" checkbox.
3. Choose Develop > Show Web Inspector.
Mozilla Firefox
From the menu bar, select Tools > Web Developer > Inspector.
You can also simply right-click on any page element and select Inspect.
For example, in the animated gif below, we can see that Chrome developer tools has
collapsed the head element and it looks like this page doesn't have much HTML on it—
but if we expand the head by clicking on the little triangle dropdown, there are a bunch
of other elements nested inside:
(As in Chrome Developer Tools, the "quote marks" just indicate a text element and
aren't literally in the page.)
<!DOCTYPE html>
<html>
<head>
<title>Listy list</title>
<style>
p { color: red; }
ol { display: flex; }
li { margin: 20px; }
</style>
</head>
<body>
<p>These are some red red words.</p>
<ol>
<li>Apple apple berry berry sauce sauce sauce.<br>
Cook it on the stove and serve it to your boss.
<li>Betty bought a bit of butter,
but the butter's burning bitter.<br>
<li>Crisp cookies claim Charlie's cookout
crown.<br> Clever Clara
clocked the crows at <em>c</em> (clearly
connivance!)
<li>Daring drivers drove down Devil's Ditch in
Dodges.
<li>Evil eggs explode everywhere. Eww!
</ol>
</body>
</html>
By the way, when styles are applied directly to an HTML element using
the style attribute, these are called inline styles.
The idea is that the style is being applied directly in the same line as the HTML element
that it is styling. (In contrast, when we use the style element, the style is separated—
it's not in the same line.)
The HTML in the workspace below has three paragraphs of text. Add a style attribute
(an inline style) to each element to make it a different color.
# ⚠️Spoiler alert!
Our solution is below. You'll learn more effectively if you give the exercise a strong
effort before comparing your code with ours!
----
Solution
Here's our code:
Our solution is below. You'll learn more effectively if you give the exercise a strong
effort before comparing your code with ours!
----
Solution
<p style="text-align: center">This is a haiku</p>
<p style="text-align: center">So I want each line centered</p>
<p style="text-align: center">Let’s make it stylish</p>
Question 2 of 2
Here is a variation on the above code:
color: blue
text-align: center
But what if you wanted to do both of these things at the same time? This turns out to be
pretty easy. All you have to do is put one after the next—and use a semicolon ; to
indicate where one ends and the next begins:
In the workspace below, give this a try! We encourage you to try to type this from
memory (referring back to the above example when you need to), rather than copying
and pasting it into the workspace. The act of trying to type it in for yourself will help you
build a stronger memory.
Note that when we only have one property, the semicolon is optional—we can
either include it or not and it won't make a difference. In other words, we can
do this:
Or this:
And it will work the same. It's only when we have more than one that
we must include a semicolon to separate each of them:
If we left out the semicolon at the end of blue; , this line would not work
correctly:
If you try this in the workspace, you'll see that neither of these styles get
applied (the text will be neither blue nor centered). So if you find that your
styles are not showing up, check for small syntax errors—like a missing
semicolon!
OK, so you've tried out inline styles that use the style attribute; now let's look at how
to apply styles using the style element.
Quiz Question
Just to make sure it's fresh in your mind, which of these examples use
inline style attributes and which use a separate style element?
Separate style element
Inline style attribute
Code
How is the style applied?
<p style="text-align: center;">I'm centered!</p>
<style>
p { text-align:center; }
</style>
<p>I'm centered!</p>
Submit
In the workspace below, you'll find some p elements that have been centered using
inline styles.
Remove the three inline styles and, instead, use a single, separate style element to
center all of the p elements.
Start Workspace
Workspaces will shut down after 30 minutes of inactivity. Any running processes will
be stopped.
Start Workspace
## ⚠️Spoiler alert!
Our solution is below. You'll learn more effectively if you give the exercise a strong
effort before comparing your code with ours!
----
Solution
<style>
p { text-align: center; }
</style>
<p>This is a haiku</p>
<p>So I want each line centered</p>
<p>Let’s make it stylish</p>
CSS Rulesets
A CSS ruleset is made up of a selector and a declaration block.
Selectors
Selectors indicate which HTML elements the ruleset will apply to. For example, consider
this ruleset:
li {
color: green;
In this example, li is the selector—meaning that whatever we put between the curly
braces { } will be applied to all list item li elements on the page.
Declaration Blocks
A declaration block describes how the ruleset will modify the elements. In the above
example, the declaration block is saying that all li elements will be modified by
changing their font colors to green.
A declaration block can have multiple declarations inside of it. For example, consider
this ruleset:
p {
background-color: blue;
font-size: 20px;
{
background-color: blue;
font-size: 20px;
}
And this declaration block contains two individual declarations. One is:
background-color: blue;
font-size: 20px;
background-color: blue;
CSS Selectors
CSS selector. Tell the browser which elements the rule will affect.
h1 {
font-family: serif;
}
Example:
//HTML
<div class="container">
</div>
//CSS
.container {
In this example, the class name is container . Any element with the class
attribute container will receive the styling of border: 1px solid black; . So for
example, if you added class="container" to the paragraph element, that would also
get this same styling.
ID
An ID is when you'd apply characteristics to one element. To create an ID, you need to
use the attribute id in the HTML opening tag. Then in your CSS, you use the same
name you gave the id attribute in your HTML, except you place a # in front of it.
Example:
//HTML
<h1 id="main-heading"> Welcome to My Page! </h1>
//CSS
#main-heading {
background-color: orange;
}
If you want to apply a style to more than one element, you should always use a class.
You should only use an ID to style one element. IDs are unique.
If you like, you can download this review as a PDF by clicking here(opens in a new
tab).
Here are some important CSS terms that you've learned so far:
DOM (or Document Object Model) is a tree structure with a node for each
HTML element, piece of text, image, and any other object in the web page.
Selectors are one part of the CSS ruleset. They indicate which HTML
element(s) the rule is for.
Example:
h3 {
text-decoration: underline;
Declaration blocks are the other part of the CSS ruleset, they say how the
rule will modify the elements indicated by selectors.
In the above example, the declaration block includes the declaration of text-
decoration: underline;.
.navigation-links {
font-weight: bold;
color: aqua;
#footer {
width: 100%;
background-color: purple;
li {
font-size: 10px;
margin-bottom: 10px;
type is the simplest kind of selector. It indicates the name of one type of
HTML element (such as em or li).
class in HTML is an attribute that groups elements together that you want to
have the same style.
Class selectors are used to set the style for multiple HTML elements that have a
particular value for the class attribute. You can name a class anything you want! Class
selectors are written with a dot before the class: for elements such as <p
class="blue"> , the class selector is .blue .
ID selectors are used when the style is being applied to one HTML element, which has
to have an id attribute. There can be only one element with a particular id on a page.
You can also choose any name you want for an id , just like a class . ID selectors are
written using a # sign: for an element such as <div id="sidebar"> , the id selector
is #sidebar .
Use this HTML document to answer the quiz below.
<html>
<head>
<style>
.special {
color: purple;
}
p {
font-style: italic;
}
#important-information {
font-weight: bold;
}
</style>
</head>
<body>
</body>
</html>
My to-do list needs more work. I'd like to highlight the most important tasks in yellow.
And I'd also like to cross out the tasks that I've already done. It should look something
like this:
Start Workspace
Workspaces will shut down after 30 minutes of inactivity. Any running processes will
be stopped.
Start Workspace
## ⚠️Spoiler alert!
Our solution is below. You'll learn more effectively if you give the exercise a strong
effort before comparing your code with ours!
----
Solution
<style>
li {
font-size: 1.5em;
}
.highlight {
background-color: yellow;
}
.done {
text-decoration: line-through;
}
</style>
<h1>To-do:</h1>
<ul>
<li class="done">Buy milk
<li>Buy eggs
<li class="done">Buy flour
<li class="highlight">Make waffles
<li>Do laundry
<li class="highlight">Learn to code
<li class="highlight">Build a robot
</ul>
So now the important items on my to-do list are highlighted, and the completed items
are crossed out. But what if I want to have an item that is marked as both
important and completed? Like this:
In other words, how can we apply two classes to one element?
This turns out to be pretty easy---we simply put in both of the class names, separated
by a space. Like this:
Start Workspace
Workspaces will shut down after 30 minutes of inactivity. Any running processes will
be stopped.
Start Workspace
Workspaces may take up to 5 minutes to start.
View Active Workspaces
You can do that by simply adding a couple more declarations to the ruleset for
the highlight class:
.highlight {
background-color: yellow;
font-weight: bold;
color: red;
Notice that if we were using inline styles, we would have to apply these changes on
every line of code with an li element. Here we only have seven—but you can imagine
how tedious this would get if we had a long list and needed to update the styles on
hundreds of list items!
Fortunately, because we have separated the style information, we can make the
changes in only one place (on the highlight ruleset) and they will automatically be
applied to all of the elements that have the highlight class. This is one of the things
that makes CSS so powerful!
A style applied at a lower level can override a style at a higher level. For instance, if
the body has color: red but a paragraph within the body has color: blue, the
blue will apply to that paragraph and to any elements inside it:
<style>
body { color: red; }
p { color: blue; }
</style>
<body>
<p> This will be blue, not red. <br>
<em> Same with this. </em> </p>
</body>
<html>
<style>
body { font-style: italic; }
h1 { color: purple; }
body { color: green; }
p { color: orange; }
</style>
<body>
<h1> This is a very stylish document! </h1>
<h2> The styles are cascading </h2>
<p> So what color am I? </p>
</body>
</html>
Revisiting the div Element
In the video, you may have noticed that Kelly used the div element to create
the boxes. We looked briefly at the div element earlier, but it has been a
while—so, let's review the key ideas.
It's used to divide up the page. The name div is short for division,
because that's what this element is for—you can use it to divide up the page
into different sections.
It has an "invisible box" around it. Like the paragraph p element, the
division div element has an invisible box around it—and just like p , it can
have a border, a margin, a width, a height, and so on.
It is a generic container. A p element is specifically meant to contain text. In
contrast, the div element is a generic container, meaning you can put
whatever other elements you want inside. You can use the div element to
organize the content and divide the page into sections.
<div>
</div>
<div>
Example
</div>
<div class="fancybox">
Example
</div>
And then, in our CSS, we can use a class selector with the same
name .fancybox to add whatever styling we want. Here's the example shown
in the video:
.fancybox{
margin: 1em;
padding: 0.5em;
width: 100px;
height: 50px;
float: right;
The div element is used all the time in HTML to structure web pages, so
you'll be seeing a lot more of it as you continue. On this page, we'll just be
using the div element to play around with boxes and adjust the margin,
padding, border, etc.
Let's add a second box. But let's make it small, and red. Let's also put some margin
around both boxes, so that they aren't mashed right up against each other.
Workspaces will shut down after 30 minutes of inactivity. Any running processes will
be stopped.
Start Workspace
## ⚠️Spoiler alert!
Our solution is below. You'll learn more effectively if you give the exercise a strong
effort before comparing your code with ours!
----
Solution
<style>
.blue_box {
border: 10px solid blue;
padding: 0.5em;
margin: 0.5em;
width: 150px;
height: 100px;
}
.red_box {
border: 10px solid red;
padding: 0.5em;
margin: 0.5em;
width: 20px;
height: 20px;
}
</style>
Now let's try out the float property. To start with, just try adding float:
right; to the blue_box class.
Start Workspace
Workspaces will shut down after 30 minutes of inactivity. Any running processes will
be stopped.
Start Workspace
.blue_box {
border: 10px solid blue;
padding: 0.5em;
margin: 0.5em;
width: 150px;
height: 100px;
float: right;
}
Let's play with the boxes a little more.
But instead of using text for the contents, we could use the other box. To do
this, we simply have to nest the div for the red box inside the div for the
blue box (replacing the current contents Hooray, a box! ).
Like this:
<div class="blue_box">
<div class="red_box">
</div>
</div>
Interpreting percentages
Here is a piece of HTML with CSS. Use it to answer the question below!
<style>
.outer {
width: 300px;
border: 5px dotted orange;
}
.inner {
border: 5px solid blue;
width: 50%;
}
</style>
<div class="outer">
<p> This box has a box inside!
<div class="inner">
<p> How wide is the inner box,
<em>including</em> its border?
</div>
</div>
CSS Reference
You can find the MDN CSS reference here:
https://developer.mozilla.org/en-US/docs/Web/CSS/Reference
<style>
p{color:blue;}
</style>
But you would not want to do both at the same time! In other words, when you place
your CSS in a stylesheet, you don't need to use a style element—you can just put
the CSS directly into the stylesheet with nothing around it:
p{color:blue;}
This is what you should do for the exercise below—simply put the CSS directly into
your style.css file (and don't use the style element).
Linking stylesheets
To link to a stylesheet in your HTML file, add a link element to the head of the
HTML file. The syntax for the link element is just like this:
If you are linking to a stylesheet located on another web server, you will use a full URL
in the href attribute. If you're linking to one that's in the same directory as your HTML
file, you can just use the filename as a relative URL.
For example, in the workspace below, you can hover over the plus button and
select New File—and then simply name the file styles.css .
Troubleshooting
When you first start using a separate CSS stylesheet, it is very common to have trouble
getting things to work.
If you have made both your .html file and your .css file and the styles just don't
seem to be showing up, you can try copying and pasting our code below into your files.
<head>
</head>
<p>
</p>
p {color:purple; font-size:24;}
(Yes, your styles.css file can really contain just that one line and nothing else!)
Linking Trouble
If you still don't see the styles changing, it's probably not linked correctly. There are two
common mistakes that can happen with the link element.
Mismatched Names
The name of the file in the link element must match the way you named your CSS
file exactly. For instance, if you name your file style.css , but then link
to styles.css (with an s at the end of styles ), it will not look in the right location
to get the style info. The names must match exactly for the link to work.
In the gif below, we can see the names don't match and the styles are not showing up.
Once the name is fixed, it works perfectly.
Note that we don't have to call the file style.css specifically—we could call
it hippopotamus.css if we wanted to. It doesn't matter what we call the file, as long as
the names match. (But please only call it hippopotamus.css if you have a good
reason… 🦛)
Wrong Filepath
If the name is correct and it is still not working, check to make sure the HTML file and
the CSS file are in the same folder. In the gif below, style.css was created in a
separate folder, called My Folder so the styles are not showing up on the page. To fix
this, we can either move the file out of that folder or we can edit the path in
the link element to include the folder name, as in My Folder/style.css .
Practice: Three Ways to Style
LessonDownloads
At this point, we've learned three different ways to apply CSS styles to our HTML
elements. This can get a little confusing, so on this page we'll review all three
approaches. Try to notice what is different in each approach, and what is the same.
As we saw earlier, we can't put CSS code directly in our HTML, because then the
browser will think it is HTML. Instead, we have to have a way of signaling that the code
should be interpreted.
Question 1 of 5
Here's an attempt to apply a CSS style to some text:
color: blue;
<p>Hello!</p>
Question 2 of 5
OK, so this code ...
color:blue;
<p>Hello!</p>
... doesn't work. But what actually happens when the browser tries to render this
page? (Feel free to try it out for yourself and see!)
It tries to apply the CSS style, but since there is no p selector, it doesn't know what to
apply the style to.
It interprets color:blue as text and displays it on the page, just like the
text Hello! .
It causes the page to fail (the page won't load in a browser until this is fixed).
Submit
Question 3 of 5
First approach:
<p style="color:blue;">Hello!</p>
Yes, it works!
No, this is broken.
Submit
Question 4 of 5
Second approach:
<style>
p{color:blue;}
</style>
Yes, it works!
Question 5 of 5
Third approach:
Add a link element in our HTML file that points to a separate CSS file (also
called a stylesheet):
p{color:blue;}
Yes, it works!
OK, now it's your turn! In the workspace below, you'll see that someone tried to apply
some styles to some text, but it's not working. See if you can fix it!
Try using all three of the approaches we just discussed, so that you can see them side-
by-side:
Make the paragraph of text centered using the style attribute to apply
an inline style.
Make the text blue using the style element with a type selector.
Make the text big using a linked CSS file (stylesheet). Note that the stylesheet
has already been created for you, but you'll need to add the style to it, and
add the correct link element to your HTML.
Note: Of course, you would normally not use all three of these approaches at the same
time like this—this is just for practice.
Start Workspace
Workspaces will shut down after 30 minutes of inactivity. Any running processes will
be stopped.
Start Workspace
## ⚠️Spoiler alert!
Our solution is below. You'll learn more effectively if you give the exercise a strong
effort before comparing your code with ours!
----
Solution
The code in the HTML file should look like this:
<style>
p {color: blue;}
</style>
p {
font-size: 24pt;
In school, you may have learned that the primary colors are red, yellow, and
blue. But in the video, Kelly said they are red, green, and blue. What's up with
that?
It has to do with how we are mixing the colors. When we mix a material
like paint the physics of how the color will look behaves very differently from
when we mix together wavelengths of light (which is what we need to do when
lighting up pixels in a computer screen). If you're super curious, you can check
out this article about primary colors from How Stuff Works(opens in a new
tab).
But for these lessons, all you really need to remember is that computers
use RGB colors—that is, red, green, and blue.
Additional Resources
If you were extremely observant you may have noticed that at the end of the
video (above), Kelly mentions HSL color. We didn't mean to get into that
here, but if you're curious, HSL stands for Hue, Saturation, and Lightness. If
you like, you can check out the w3schools.com page on HSL color(opens in
a new tab). If you scroll down on the page, they even have a live example you
can play with to see how HSL colors work.
Kelly showed three different ways to represent color values in the code. For
example:
#ffffff
Although they look different on the surface, they all work essentially the same
way.
In all cases, we need to give the amount (or you could say the intensity) of each
of the three primary colors: red, green, and blue (RGB).
We've been using the words "amount" and "intensity". What we really mean by this is
the brightness of the light. Higher values indicate brighter (more intense) light, and lower
values indicate darker (less intense) light.
By mixing different levels of red, green, and blue, we can get different colors, at different
levels of brightness.
By the way, this way of thinking about light isn't limited to our code—if we were to get
real, physical lights and overlap them, we would get similar results.
Red, green, and blue light overlap to create secondary colors and white light.
(Public domain image from Wikimedia Commons(opens in a new tab))
In the image above, notice that if we mix equal (and intense) levels of all three colors,
we get white light. And you can probably imagine that if we lowered the intensity of all
three colors by equal amounts, we would get various shades of gray.
These look strange, but they work the same way. Each pair of digits is a
number that gives the intensity of red, green, or blue. The reason these values
look strange is because they're in a different number system—instead of
the decimal(opens in a new tab) system that we are used to working with,
these numbers are given in the hexadecimal(opens in a new tab) system.
You don't need to have a deep familiarity with hexadecimal to be able to use
hex color values on your pages—but we'll briefly explain the basics so that
you can get a feeling for how they work.
Here is how we would show the numbers 0-15 in decimal and hexadecimal:
| Decimal | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
|---|---|---|---|---| | Hex | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
Notice that it's the same until we get to 10, which is represented as A in
hexadecimal.
Like we mentioned, each pair of digits in a hex RGB value gives the intensity
of red, green, or blue. This ranges from 00 , meaning zero intensity, to ff ,
meaning maximum intensity. In fact, the number ff is the same as the
decimal number 255 .
This may seem very alien, but take a try at the following exercise—just to get
a feeling for how hex values work.
There are so many CSS properties out there, it's difficult to know (or remember) them
all! And even if you do remember the property you want, you may not be able to recall
the exact syntax. (Was it text-align: center; or text-align: centered; ?
Even experienced developers don't have every single property memorized. Fortunately,
it's usually easy to find what you need by looking it up in the documentation or using
your favorite search engine.
Most of the time, you can simply type in "CSS", followed by some words related to the
property. For example, if you want to know how to set the background color, searching
for "css background color" will turn up the results you're looking for.
First thing
Second thing
And you want to change the style of the list (for example, to use square bullets instead
of round ones). What CSS property could you use?
Submit
Font vs Typeface
If you have worked in a word processor (like Microsoft Word, for example), you may
have seen things like Times New Roman, Courier, Arial, or Helvetica. For example:
In word processors (and everyday speech) we would call this the "Times New Roman
font". But technically, "Times New Roman" is not a font—it is a typeface.
Wait, what?
Most typefaces have multiple versions that look a little different from one another. For
example, we have:
And also:
Font Families
Another term we can use for a typeface (or group of related fonts) is a font family.
That's the term that CSS uses.
In other words, to change the typeface of our text, we can use the font-
family property. Like this:
font-family: Helvetica;
Note: You might notice that one of these has quotes " " while the other does not. The
quotes are recommended for font families that have spaces in the name. They help
ensure that the name is read as one thing ( "Times New Roman" ), rather than potentially
three separate things ( Times , New , and Roman ). The spaces don't usually cause a
problem, so this is a recommended practice, not a requirement (you can leave them off
and it will typically still work).
Monospace as an Example
Compare these three examples:
This is an example
This is an example
This is an example
Each of these uses a different font family (or typeface). If you're curious, they
are Courier, Courier New, and Source Code Pro.
Even though they are different font families, they are all examples of monospace font
families. In a monospace font family, every character is the same width—so, for
instance, these two letters are the same width:
i
m
Monospace font families are really useful for writing code because they ensure that
everything lines up exactly as expected:
So if we were putting some code on a web page, we might not care exactly what font
family it uses, but we would want to make sure it is some kind of monospace.
Serif
Sans-serif
Cursive
Fantasy
Monospace
In CSS, we can use a generic font family in place of a specific font family. For example,
this line specifies the specific font family Courier:
font-family: Courier;
In contrast, this one simply says to use whatever monospace font family is available
font-family: monospace;
Font Stacks
Since we don't know what font will be available to each user, it's a good idea to give
multiple options. CSS allows us to do this by using a font stack, like this:
As you can see, we simply list the font families that are OK to use, with the most-
preferred choice first. If that choice isn't available, it will try the next one, and so on.
A common (and very good) practice is to put the generic font family at the end of the list,
like this:
This way, if none of the other font families are available, the browser will still use the
right general type.
Task List
Use a generic font family for the last one in the stack.
Start Workspace
Workspaces will shut down after 30 minutes of inactivity. Any running processes will
be stopped.
Start Workspace
Our solution is below. You'll learn more effectively if you give the exercise a strong
effort before comparing your code with ours!
----
There are many different font families you can use. Here are a few different examples of
what your CSS file might look like. Each one provides a couple of specific font families
and then ends with the relevant generic font family:
Monospace:
p{
}
Serif:
p{
Sans-serif:
p {
font-family: Verdana, Helvetica, sans-serif;
}
Additional Resources
If you're curious to learn more, you can find additional details and some
interactive examples in the MDN Web Docs on the font-family
property(opens in a new tab).
When you need to know what font families to use in a stack, you can check
out cssfontstack.com(opens in a new tab). When you click on a font family, it
will tell you what other fonts you might want to put in the same stack, and it
even shows the CSS you would use for each one.
In addition to the font family, there are a bunch of other properties we can use
to style our text.
Font Size
To change the font size, we can use the font-size property, like this:
font-size: 24px;
font-weight: bold;
Italic
To make the text italic, we can use the font-style property:
font-style: italic;
Underline
To make the text underlined, we can use the text-decoration property:
text-decoration: underline
Font Shorthand
LessonDownloads
font-style: italic;
font-size: 24px;
font-family: Times;
1. font-style
2. font-variant
3. font-weight
4. font-size
5. font-family
If you're able to memorize that, great! If not, you can do what Karl does and
just look it up when you need it. You can find it in this CSS-TRICKS
article(opens in a new tab) , listed near the top of the CSS font property
documentation from W3Schools(opens in a new tab) —or simply Google "font
shorthand order".
Wait, what if I leave something out?
There are two properties that are required when you use
the font shorthand: font-size and font-family . Everything else is optional. If
you leave out one of the optional properties, it will just use the default.
Since the font-style isn't specified, it defaults to normal (i.e., not italic).
<div>
</div>
</div>
One reason to nest boxes like this is so that you can use one of the
boxes as a container for the other boxes. To get an idea of why this
would be useful, let's take a look at an example.
In the workspace below, you'll find three boxes stacked vertically (notice
that they're currently not inside a container). You'll also see that there's
a linked stylesheet, and that we've styled the boxes using some different
classes.
Suppose that we want to get all three of the boxes to stay in a vertical
stack, but move over to the right side. How can we do this?
Well, first let's try this: Add float:right; to the .box class, so that the
boxes float on the right side of the page. Then answer the questions
after the workspace.
OK, so trying to float the boxes on the right individually didn't do what
we wanted—they ended up in a horizontal row, rather than staying in a
vertical stack.
To solve this, we can put the three boxes inside a fourth box. By
wrapping them in a container like this, we can then move the container
around without changing the arrangement of the boxes inside.
Code for containers.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Containers</title>
<link rel="stylesheet"
href="containers_style.css">
</head>
<body>
<div class="container">
</body>
</html>
Additional Resources
It's not required for this course, but if you want to understand the concepts and
options in more detail, you may enjoy reading this documentation from Mozilla
for the flexible box layout (aka "flexbox")(opens in a new tab) . This is pretty
advanced, so don't be surprised if you find it challenging. If you want to, you can
play with the workspace above (or one of your local HTML files if you're
experimenting on your own computer) to see what you can do. For example, can
you use the documentation to figure out how to write flexbox code that results in
a two-column layout?
When using flexbox, it's important to understand how the size of the container
element affects the layout of the boxes that are inside of it.
You may have noticed that in Kelly's code, the width of the .container class is
set to 100% . That means the container will be as wide as whatever it's inside—
in this case, the container is inside the body element of the page, so it will take
up the full width of the page.
To see this more easily, we can add a border to the container class.
In the workspace below, add border: 5px solid black to the .container class.
Start Workspace
Now that we can see the size of the container element, let's try changing the
width.
Quiz Question
Currently the width is set to 100%. Try changing it to 200 pixels ( 200px ).
What happens?
⚠️Make sure you enter 200px and not just 200 . If you leave off
the px you'll get very different results!
The first two boxes stay where they are, but the third box wraps to the second
line.
All three boxes stay where they are (in a single horizontal line).
All three boxes stay where they are (in a single horizontal line) and resize
(becoming narrower).
Submit
In case you need it, here's the full CSS code with the changes we just made:
.container{
width: 200px;
display: flex;
flex-wrap: wrap;
border: 5px solid black;
}
.box{
width: 100px;
height: 100px;
text-align: center;
font-size: 30px;
font-weight: bold;
font-family: sans-serif;
}
.red{
background-color: red;
}
.green{
background-color: green;
}
.yellow{
background-color: yellow;
}
Let's get some more practice with flexbox. Your job here is to replicate this
checkerboard design:
We've given you a little starter code, but it's up to you to create all the boxes and
add the correct CSS.
This is very similar to the code we just went over on the last page, so if you get
stuck you can always look back at that (or check out our solution code below).
Start Workspace
Workspaces will shut down after 30 minutes of inactivity. Any running
processes will be stopped.
Start Workspace
Our solution is below. You'll learn more effectively if you give the exercise a
strong effort before comparing your code with ours!
----
Solution
Code for flexbox_practice.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Flexbox practice!</title>
</head>
<body>
<div class="container">
</body>
</html>
Ruleset Syntax
The basic syntax of a CSS ruleset has two parts: a selector, and a group of rules,
each of which consists of a property name and the value of that property.
selector {
property: value;
}
The selector is written first, and then the rules are written inside { curly
brackets } . Each rule's property and value are separated by a : colon, and the
rule always ends with a ; semicolon.
Selectors
The selector indicates which HTML elements the rule will apply to. You've seen a
few different sorts of selector: the element selector, the class selector,
the id selector, and the descendant selector.
A type selector applies to every HTML element of a particular type, such
as p or em . This selector will apply to every p element:
p {
color: blue;
}
A class selector applies to all elements that share a class attribute. The class
selector is written starting with a . (dot):
.narrow {
width: 20%;
}
In order for the class selector to apply, there have to be HTML elements on the
page that use that class attribute:
<div class="narrow">
</div>
width: 20%;
float: left;
}
Within an HTML page, there should be only one element with that id attribute
value.
<div id="sidebar">
</div>
color: pink;
}
The above selector will apply to a elements (hyperlinks), but only those inside
an li element (list item):
<ul>
</ul>
This rule applies the value red to the property color , and the value larger to
the property font-size .
Some properties allow values that are more than one word long, such as
the font property:
body { font: 12pt bold Consolas, Monaco, monospace; }
Font stacks
The font-family and font properties allow you to specify a font stack, a list of
font options separated by , commas. The browser will use the first font in the
stack that is available on the user's system. Usually the last font in the stack
should be a generic font name, such as serif , sans-serif , or monospace .
Colors
There are several ways to specify a color in CSS. Three common ones are hex
codes, rgb triples, and color names.
.orange {
color: #ff9900;
}
.pink {
}
.chartreuse {
color: chartreuse;
}
Flexbox
To change the browser's layout from the default document-based layout to the
flexible box layout, set display: flex on a container element (one that has other
elements inside it).
.outer {
display: flex;
}
.inner {
width: 100px;
padding: 10px;
}
Flexbox can be heavily customized! The above will cause .inner HTML elements
to be packed in a row within the .outer element:
<div class="outer">
I am a box.
I am another box.
Hey, I am a box, too! Boxes rock.
Let's be boxes together. Yay, flexbox.
In this last part of the lesson, we're presenting you with a challenge very similar
to what a real web developer might encounter: You'll be given a design and
asked to apply your new CSS skills to reproduce it.
This exercise is definitely harder than anything else we've done in this lesson!
We encourage you to give it a try and really experiment with your CSS to see if
you can get the desired result—but also we don't want you to get stuck or
discouraged.
Kelly and Karl each provide full solutions on the next page, so regardless of
whether you solve it or only get part way there, you should move ahead and
check out how they approached it.
In the workspace below, try replicating the design of the tic-tac-toe board
shown in the video. There's also a copy of the image below the workspace.
To do this, you'll only need to add CSS rules to the file tictactoe.css. (You
don't need to edit the HTML file at all!)
The ul and the li elements have boxes just like the div elements we've been
working with. And you can think about the ul as a container for the li elements.
Like Karl said in the video, you will probably run into a couple of places where
you need to look up CSS properties you haven't encountered before. For
example, you'll need some way of hiding the bullets in the bulleted list. (Karl
had to look that up too when he solved the challenge.) Google is your friend
here!
As with most problems in computer science, there's more than one way to solve
this! We feel this is an important idea, so we've included both Karl's approach
and Kelly's solution as well.
Karl's Solution
Show TranscriptSummarize Video
Kelly's Solution
Note: In this next video, the code editor shows font-size: 40px , but Kelly's code
should actually have said font-size: 80px (the 40px font is too small and won't
appear to be vertically centered).
Show TranscriptSummarize Video
Congratulations!
If you've made it this far, then you've picked up some genuinely challenging
concepts, including some ideas—like tree structures—that you would be
learning if you were enrolled in a university-level Computer Science (CS)
curriculum.
By learning CSS and HTML, you've picked up some very practical web
development skills, such as how to:
Along those lines, the final project for this course (coming up next) is how
you'll demonstrate that you really know how to use the tools you've just
learned about. Can you use HTML and CSS to actually make a thing? That is
the real test!
But don't worry, you can't fail this test. When you submit your project, a
Udacity reviewer will check it over carefully. If something isn't quite right,
they'll mark it as Requires Changes ❌ and leave you some helpful comments
so you know what needs to be changed.
Once it all looks good, they'll mark it as Meets Specifications ✅ and you will
have officially passed the course!
So with all of that in mind, we encourage you to just jump in and have fun with
it! And if you don't pass the first time (or second or third time!), don't feel bad
and don't give up—just check out what your reviewer has told you and use
their feedback as an opportunity to get better and grow your new skills. 🌱
Intro to JavaScript
LessonDownloads
Today, JavaScript is used for all sorts of applications - from programming robots to
writing game scripts. Even some code editors were built with JavaScript.
If you're familiar with using HTML and CSS to create web pages, JavaScript is the final
piece you'll need to make your websites come to life!
Prerequisites
There are no prerequisites for this course.
Where Did JavaScript Come From?
The first version of JavaScript was created in just ten days 1995 by Brendan
Eich(opens in a new tab) . Eich was working on Netscape Navigator, one of the
Internet's first web browsers. Eich's goal was to add the capability for dynamic web
pages, which, at that time, were simple pages of HTML and CSS.
JavaScript Today
JavaScript has grown to be one of the most popular programming languages globally
and is considered one of the foundational pillars of front-end web development.
You can read more about the current standards for JavaScript in MDN Web Docs:
JavaScript(opens in a new tab)
Want to Learn More?
Read a little history of JavaScript in Wikipedia(opens in a new tab) .
TIP: HTML and CSS are markup languages*. Markup languages are used to describe
and define elements within a document. JavaScript is a* programming language*.
Programming languages are used to communicate instructions to a machine.
Programming languages can be used to control the behavior of a machine and to
express algorithms.*
"Julia"
Writing code in the console is a great way to test out simple bits of code, but
as your code gets larger the browser user interface can be awkward. We
recommend using a text editor like VSCode(opens in a new tab) , Atom(opens
in a new tab) , or Sublime Text(opens in a new tab) to write your code and
pasting your code in the console when you're ready to run it.
We'll also provide workspaces for the quizzes and activities in this course.
If you didn't, that's okay. Developer tools aren't always the easiest thing to find
in your browser. So, we've decided to help you out by creating this guide to
developer tools!
Google Chrome
The Chrome DevTools are a set of web authoring and debugging tools built
into Google Chrome. Use the DevTools to iterate, debug and profile your
site. Learn more about Chrome DevTools here(opens in a new tab) .
Mozilla Firefox
Firefox Developer Tools allow you to examine, edit, and debug HTML, CSS,
and JavaScript on the desktop and mobile. Also, you can download a version
of Firefox called Firefox Developer Edition(opens in a new tab) , which is
tailored for developers, featuring the latest Firefox features and experimental
developer tools. Learn more about Mozilla Firefox DevTools here(opens in
a new tab).
To open Firefox Developer Tools, either right-click on any page element and
select Inspect Element or open the Firefox settings menu in the top-right
corner of your browser window and select Developer . Alternatively, you can
use the shortcuts:
Microsoft Edge
Microsoft Edge introduced great new improvements to the F12 developer
tools seen in Internet Explorer. The new tools are built in TypeScript and are
always running, so no reloads are required. In addition, F12 developer tools
documentation is now fully available on GitHub(opens in a new tab) .
Safari
For any Mac users, Safari includes Web Inspector, a powerful tool that makes
it easy to modify, debug, and optimize a website for peak performance and
compatibility on both platforms. Learn more about Safari Web Inspector
here(opens in a new tab) .
Opera
Opera is a fast, lean, and powerful web browser. You can open Developer
tools in Opera using the following keyboard shortcuts:
Command + Option + i (Mac)
Ctrl + Shift + i (Windows/Linux).
console.log("hiya friend!");
NOTE: You may see some errors or warnings in the console from the site you're visiting
-- and that's okay! Warnings are very common and will not affect the code you write in
this course.
Troubleshooting
For Chrome users, if you don't see the output, click “Default levels” in the console and
make sure that "Info" is checked. Congratulations! You performed the log action on
the debugging console .
The message you’ve logged is "hiya friend!". hiya friend! is a string (a sequence of
characters).
Give It a Try!
Let’s use console.log to do something a little more interesting. Here’s a block of
JavaScript code that loops through the numbers 0 through 9 and prints them out to the
console:
console.log(i);
Prints:
0
1
2
3
4
5
6
7
8
9
This is called a loop.
Based on this loop's settings, any code written inside the curly brackets {...} will be
repeated ten times. In this case, console.log prints out the value of i each time the
loop runs. Don't worry if you're not sure about what the syntax means at this point. Later
in this course, you will learn more about how and when to use loops.
console.log Demo
Browser console has other methods that can be very useful when poking around. For
example, try typing console.table(["orange", "strawberry", "banana"]) in the
console. This method will create a table and display the data in it.
You are welcome to explore the console.dir method on your own. Don't be afraid to
tinker!
For more information you can also refer to MDN documentation: console(opens in a
new tab).
JavaScript demo
So you saw how to use console.log to print a message to the JavaScript
console. Now, let’s see how you can use the console as a sandbox to test a
new line of JavaScript in the browser.
Open the Daring Fireball website(opens in a new tab) in a new tab and in that
tab also open up developer tools. Then paste the following code:
document.getElementsByTagName("h1")[0].style.color =
"#ff0000";
Removing an item
Adding up the cost of all items in the cart
Paying for the items
The code to connect your products and functions will also be supplied, so you only need
to focus on the JavaScript code.
Learned that all major browsers come with built-in JavaScript engines that
allow browsers to run and execute JavaScript code.
Practiced running JavaScript code in the browser console
Added styles to a webpage just by running code in the console
We hope you are beginning to see the power of JavaScript, and you are ready
to dive in and explore the language.
Data is Important
It helps us:
Data and data types are the building blocks of any programming language
because they help us organize information and determine how our programs
will run.
In this lesson we will learn how to define and manipulate the primitive data
types of JavaScript.
numbers
strings
booleans
undefined
null
Once you're familiar with these data types, you'll see how you can store data
in variables that you can reuse to manipulate data with your code.
Numbers
Defining a number in JavaScript is actually pretty simple. The Number data type
includes any positive or negative integer, as well as decimals. Entering a number into
the console will return it right back to you.
Returns: 3
There, you did it.
Arithmetic operations
You can also perform calculations with numbers pretty easily. Basically, type out an
expression the way you would type it in a calculator.
3 + 2.1
Returns: 5.1
We use the modulo operator to return the remainder of a division operation. This can be
very helpful in a lot of programming tasks.
Comparison Operators
Operator Meaning
== Equal to
!= Not Equal to
TIP: The values true and false have significant importance in JavaScript. These values are
called Booleans and are another data type in JavaScript. Later in this lesson, you’ll learn more
about why Booleans are so important in programming.
Before you move onto the quiz, we want to talk about something you'll see
quite often throughout this course: comments!
You can use comments to help explain your code and make things clearer. In
JavaScript, comments are marked with a double forward-slash // . Anything
written on the same line after the // will not be executed or displayed. To have
the comment span multiple lines, mark the start of your comment with a
forward-slash and star, and then enclose your comment inside a star and
forward-slash / *…* / .
/*
this is
a multi-line
comment
*/
Some of the quizzes in this course might include comments that give you hints
or instructions to complete the quiz. Pay attention to those comments!
You may see comments that are used to clarify and document complex code.
This may improve readability, but it often makes more sense to reduce the
complexity by refactoring and simplifying the code. Simpler code is often
better code.
node first-expression.js
⚠️These workspaces will autosave your code -- but it can take a few seconds.
If you try to run your code before the autosave occurs, you will see an error like this:
ReferenceError: Cannot access 'variableName' before initialization
Wait a few seconds and try to run the code again.
Start Workspace
Workspaces will shut down after 30 minutes of inactivity. Any running processes will
be stopped.
Start Workspace
Show Solution
TIP: It is correct to either use double " or single ' quotes with strings, as long as you're
consistent. The JavaScript Udacity style guide(opens in a new tab) for labs and
projects suggests using single quotes to define string literals -- but your team or
organization might follow a different style guide and prefer double quotes.
Always follow your team's style guide
String concatenation
Strings are a collection of characters enclosed inside double or single quotes.
You can use strings to represent data like sentences, names, addresses, and
more. Did you know you can even add strings together? In JavaScript, this is
called concatenating. Concatenating two strings together is actually pretty
simple!
Storing the value of a string in a variable is like packing it away for later use.
Now, if you want to use "Hello" in a variety of sentences, you don't need to duplicate
"Hello" strings. You can just reuse the greeting variable.
greeting = "Hola";
Here's why: in a small program with just a few variables, it's easy to keep track of the
variables and avoid collisions. But when you are working on a complex project or with a
large team. It is very easy to inadvertently overwrite an existing variable that is hidden in
another part of the program. let and const avoid this issue because they are only
available in the scope where they are declared. Again, more on scope later. For now,
remember:
Here's why: in a small program with just a few variables, it's easy to keep
track of the variables and avoid collisions. But when you are working on a
complex project or with a large team. It is very easy to inadvertently overwrite
an existing variable that is hidden in another part of the
program. let and const avoid this issue because they are only available in the
scope where they are declared. Again, more on scope later. For now,
remember:
So which should be your default choice? That's easy. Pick the one that gives
you more control: const . Using const means that your program will throw an
error if there is an attempt to change the value of the variable when the code
runs. If that is an intended outcome, you can go back and revise your code to
declare the variable with let . If you did not intend for the value to change,
congratulations! You've just discovered a bug that you can fix before it causes
any problems.
Naming conventions
When you create a variable, you should write the name of the variable using
camelCase: the first word is lowercase, and all following words begin with a
capital letter. Additionally, aim to use a variable name that accurately yet
succinctly describes what the data represents.
const tip = 8;
Directions:
Use this equation and the variables fahrenheit and celsius to print the Fahrenheit
equivalent of 12°C.
node convert.js
Start Workspace
Workspaces will shut down after 30 minutes of inactivity. Any running processes will
be stopped.
Start Workspace
Try the quiz on your own first. If you get stuck you can check the solution by clicking the
button below.
Show Solution
LessonDownloads
Indexing
Did you know that you can access individual characters in a string? To access an
individual character, you can use the character's location in the string, called its index.
Just put the index of the character inside square brackets (starting with [0] as the first
character) immediately after the string. For example:
"James"[0];
Returns: "J"
or more commonly, you will see it like this, using a variable:
name[0];
Returns: "J"
Show TranscriptSummarize Video
Note - This video does not have an audio. It was created as a visual to aid
learning.
Characters within a string are indexed starting from 0, where the first character is at
position 0, to n-1, where the last character is at position n-1 (n represents the total
number of characters within a string).
Quiz Question
What character will be printed to the JavaScript console after running the
following lines of code.
a
w
h
Submit
Escaping strings
There are some cases where you might want to create a string that contains
more than just numbers and letters. For example, what if you want to use
quotes in a string?
Because you need to use quotes to denote the beginning and end of strings,
the JavaScript engine misinterprets the meaning of your string by
thinking "The man whispered, " is the string. Then, it sees the remaining please
speak to me."" and returns a SyntaxError .
If you want to use quotes inside a string, and have JavaScript not
misunderstand your intentions, you’ll need a different way to write quotes.
Thankfully, JavaScript has a way to do this using the backslash character
( \ ).
If you forget to use the backslash to escape characters, then the JavaScript engine can
misinterpret your strings.
Escaping characters
In JavaScript, you use the backslash to escape other characters.
Escaping a character tells JavaScript to ignore the character's special meaning and just
use the literal value of the character. This is helpful for characters that have special
meanings like in our previous example with quotes "…" .
Because quotes are used to signify the beginning and end of a string, you can use the
backslash character to escape the quotes in order to access the literal quote character.
💡 By using the backslash to escape characters, the JavaScript engine can understand
the meaning of your strings.
Special characters
Quotes aren’t the only special characters that need to be escaped, there’s
actually quite a few(opens in a new tab) . However, to keep it simple, here’s a
list of some common special characters in JavaScript.
Code Character
\\ \ (backslash)
\n newline
\t tab
The last two characters listed in the table, newline \n and tab \t , are unique
because they add additional whitespace to your Strings. A newline character
will add a line break and a tab character will advance your line to the next tab
stop(opens in a new tab) .
Returns:
Up up
down down
Comparing strings
Another way to work with strings is by comparing them. You've seen the comparison
operators == and != when you compared numbers for equality. You can also use
them with strings! For example, let’s compare the string "Yes" to "yes" .
"Yes" == "yes"
Returns: false
When you run this in the console, it returns false. Why is that? "Yes" and "yes" are
the same string, right? Well not quite.
A. Case-sensitive
When you compare strings, case matters. While both string use the same letters (and
those letters appear in the same order), the first letter in the first string is a
capital Y while the first letter in the second string is a lowercase y .
'Y' != 'y'
Returns: true
B. Internal Working
In Javascript, strings are compared character-by-character in alphabetical order. Each
character has a specific numeric value, coming from ASCII value of Printable
characters(opens in a new tab) . For example, the character 'A' has a value 65, and 'a'
has a value 97. You can notice that a lowercase letter has a higher ASCII value than the
uppercase character. If you want to know the ASCII value of a particular character, you
can try running the code below:
// Let us print
console.log(ASCII_value);
In the example above, if you wish to print ASCII values of all the characters in your
string, you would have to use Loops that we will study in later part of this course. Just
for reference, here is how you can use a loop to print the ASCII value of all characters in
a string.
console.log(my_string.charCodeAt(i));
The ASCII values of [A-Z] fall in the range [65-90], whereas, the ASCII values of [a-z]
fall in the range [97-122]. Therefore, when we compare strings, the comparison
happens character-by-character for the ASCII values.
Directions:
Create a string with the name of your favorite food. The first letter of the string
should be capitalized.
node favorite-food.js
Directions:
Fix the right side expression so it evaluates to true .
"ALL Strings are CrEaTeD equal" == "All STRINGS are
CrEaTED Equal"
node equality.js
Directions:
Build a single string that resembles the following joke.
Your joke should take the format of a question and answer. The first line
should be a question and the second line should be an answer.
Hint: You will need to use special characters to produce the following output.
node tied-up.js
Directions:
Build a string using concatenation by combining the lines from this famous
haiku poem by Yosa Buson(opens in a new tab) .
Hint: You will need to use special characters to produce the following output. For a
refresher, feel free to review the previous Escaping Strings topic in this lesson.
node yosa-buson.js
A boolean variable can take either of two values - true or false . For example,
if (haveEnrolledInCourse) {
Let's look at an example that will explain the role of a boolean variable in comparison.
const a = 10;
const b = 20;
} else {
if (1) {
console.log("This statement will always execute
because conditional is set to 1 i.e., true");
}
if (0) {
console.log("This statement will NEVER execute
because conditional is set to 0 i.e., false");
}
null refers to the "value of nothing", while undefined refers to the "absence of
value".
What is NaN?
NaN stands for "Not-A-Number" and it's often returned indicating an error with number
operations. For instance, if you wrote some code that performed a math calculation, and
the calculation failed to produce a valid number, NaN might be returned.
// calculating the square root of a negative number will
return NaN
Math.sqrt(-10)
Equality
So far, you’ve seen how you can use == and != to compare numbers and strings for
equality. However, if you use == and != in situations where the values that you're
comparing have different data-types, it can lead to some interesting results. For
example,
"1" == 1
Returns: true
and
0 == false
Returns: true. Both the operands on either side of the == operator are first converted
to zero, before comparison.
All of the above three evaluate to true. The reason for such interesting outcomes
is Type Conversion. In the case of regular comparison, the operands on either side of
the == operator are first converted to numbers, before comparison. Therefore, a '
' , false , and 0 are all considered equal. Similarly, a '1' and 1 are also considered
equal. If we don't want to convert the operands, before comparison, we have to use
a strict comparison === , that is explained below.
"julia" + 1
Returns: "julia1"
In this example, JavaScript takes the string "julia" and adds the number 1 to it
resulting in the string "julia1" . In other programming languages, this code probably
would have returned an error, but in JavaScript the number 1 is converted into the
string "1" and then is concatenated to the string "julia" .
It’s behavior like this which makes JavaScript unique from other programming
languages, but it can lead to some quirky behavior when doing operations and
comparisons on mixed data types.
int count = 1;
With a loosely typed language like JavaScript, you don’t need to specify data types;
this provides a lot more flexibility and is often faster to write. However, loose typing can
lead to errors that are hard to diagnose due to implicit type coercion.
"1" == true
Returns: true
When you use the == or != operators, JavaScript first converts each value to the
same type (if they’re not already the same type); this is why it's called "type coercion"!
This is often not the behavior you want, and it’s actually considered bad practice to
use the == and != operators when comparing values for equality.
Instead, in JavaScript it’s better to use strict equality to see if numbers, strings, or
booleans, etc. are identical in type and value without doing the type conversion first. To
perform a strict comparison, simply add an additional equals sign = to the end of
the == and != operators.
"1" === 1
Returns: false
This returns false because the string "1" is not the same type and value as the
number 1 .
0 === false
Returns: false
This returns false because the number 0 is not the same type and value as the
boolean false . Just like strict equality operator, there is also a strict non-
equality operator !== that can be used instead of != if you don't want a type-
conversion, before comparison. For example,
0 !== true
Returns: true
and
'1' !== 1
Returns: true
One thing to take notice of in all the examples you've seen so far is the use of
semicolons ; at the end of each line. Semicolons make it clear where one statement
ends and another begins. This is especially handy when multiple lines of code are
written on the same line (which is valid JavaScript, but definitely not recommended!).
For instance:
Not adding semicolons to the end of each line can cause bugs and errors in your
programs. JavaScript does have ways to occasionally predict where
semicolons should be, but just like how type coercion can result in some unexpected
quirky behavior in JavaScript, it's good practice to not depend on it.
red blue
where "red" is the value of thingOne and "blue" is the value of thingTwo . Don't
forget to use semicolon at the end of each statement!
node semicolons.js
Start Workspace
Workspaces will shut down after 30 minutes of inactivity. Any running processes will
be stopped.
Start Workspace
Try the quiz on your own first. If you get stuck you can check the solution by clicking the
button below.
Show Solution
Directions:
Create a variable called fullName and assign it your full name as a string.
Print the value of the variable in the console.
Try the quiz on your own first. If you get stuck you can check the solution by clicking the
button below.
Show Solution
Directions:
Create a variable called bill and assign it the result of 10.25 + 3.99 +
7.15 (don't perform the calculation yourself, let JavaScript do it!). Next, create
a variable called tip and assign it the result of multiplying bill by a 15% tip
rate. Finally, add the bill and tip together and store it into a variable
called total .
TIP: To print out the total with a dollar sign ( $ ) use string concatenation. To
round total up by two decimal points use the toFixed() method. To
use toFixed() pass it the number of decimal points you want to use. For example,
if total equals 3.9860 , then total.toFixed(2) would return 3.99 .
node dinner.js
Directions:
Mad Libs(opens in a new tab) is a word game where players have fun
substituting words for blanks in a story. For this exercise, use the adjective
variables below to fill in the blanks and complete the following message.
node mad-libs.js
Directions:
Here are two awesome messages:
Declare and assign values to three variables for each part of the sentence that
changes ( firstName , interest , and hobby ).
Use your variables and string concatenation to create your own awesome
message and store it in an awesomeMessage variable. Finally, print your
awesome message to the JavaScript console.
We also learned how you can store data in variable and we practiced some
operations on numbers and strings.
In the next lesson, we'll build upon your new knowledge of data types and
variables to learn how to write code to solve logical problems.
DEFINITION: A flowchart is a visual diagram that outlines the solution to a problem through a
series of logical statements. The order in which statements are evaluated and executed is called
the control flow.
Flowcharts are a great way to visualize a problem before you start writing code.
Question 1 of 2
Take a look at the flowchart above. What data type would best represent (Yes/No) if you
have enough money to purchase the item?
Number
Now we compare the price to the money we have to determine if we have enough
money. We can use a JavaScript if else statement for that:
} else {
Pay attention to the curly braces! The code in the curly braces after the if statement
will run if the statement evaluates to true and the code in the curly braces after
the else statement will run if it evaluates to false .
Why does the video use var and the example code
use const ?
Although var works, it is now considered best practice to use let or const instead.
We're showing you both but you should try to use let and const in your own code.
If...else Statements
If...else statements allow you to execute certain pieces of code based on a condition, or set of
conditions, being met.
} else {
This is extremely helpful because it allows you to choose which piece of code you want to run
based on the result of an expression. For example,
const a = 1;
const b = 2;
if (a > b) {
} else {
The value inside the if statement is always converted to true or false. Depending on the value,
the code inside the if statement is run or the code inside the else statement is run, but not both.
The code inside the if and else statements are surrounded by curly braces {...} to separate
the conditions and indicate which code should be run.
TIP: When coding, sometimes you may only want to use an if statement. However, if you try to
use only an else statement, then you will receive the error SyntaxError: Unexpected token
else . You’ll see this error because else statements need an if statement in order to work. You
can’t have an else statement without first having an if statement.
Question 1 of 2
We've removed the curly braces after the if and else statements. What do you think will
happen if you run this code:
const a = 1;
const b = 2;
if (a > b)
console.log('a is greater than b');
else
console.log('a is less than or equal to b');
const a = 1;
const b = 2;
if (a > b)
console.log('a is greater than b');
a = a + 2;
else
console.log('a is less than or equal to b');
You'll get an error message: Uncaught SyntaxError: Unexpected token 'else'
The code will run as expected and return a is less than or equal to b
What We Learned
Curly braces are not necessary if you have only one line of code to execute following
an if or else statement. They are necessary if you have more than one line of code to
execute.
That being. said, in most cases, even though it is not required, it is a better practice to
use curly braces whenever you are using a conditional statement.
Learn More
Read the documentation: MDN Web Docs - if else(opens in a new tab)
More Conditionals!
In some situations, two conditionals aren’t enough. Consider the following situation.
You're trying to decide what to wear tomorrow. If it is going to snow, then you’ll want to
wear a coat. If it's not going to snow and it's going to rain, then you’ll want to wear a
jacket. And if it's not going to snow or rain, then you’ll just wear what you have on.
Flowchart for deciding what to wear tomorrow.
Else if statements
In JavaScript, you can represent this secondary check by using an extra if statement
called an else if statement.
console.log("Bring a coat.");
} else {
If it’s not going to snow, then the code will jump to the else if statement to see if it’s
going to rain. If it’s not going to rain, then the code will jump to the else statement.
The else statement essentially acts as the "default" condition in case all the
other if statements are false.
Question 1 of 2
What will be printed to the console if the following code is run?
Question 2 of 2
Looking at the following code, determine what medal Kendyll received.
Directions:
Write an if...else statement that:
node even-or-odd.js
Node.js(opens in a new tab) is an open-source backend JavaScript runtime
environment. That basically means that it will run your JavaScript code in the terminal
and return any output that is generated. We'll be using Node to run the code in our
workspaces for many of the quizzes in this course.
3 odd
14 even
5984 even
35425413 odd
⚠️These workspaces will autosave your code -- but it can take a few seconds.
If you try to run your code before the autosave occurs, you will see an error like this:
ReferenceError: Cannot access 'variableName' before initialization
Wait a few seconds and try to run the code again.
Musical groups have special names based on the number of people in the group.
Directions:
Write a series of conditional statements that:
HINT If you aren't sure how to proceed, try drawing a flow chart to outline the decision
points and how the decision is made at each point
node musical-groups.js
0 "not a group"
1 "solo"
2 "duet"
3 "trio"
4 "quartet"
Julia's plan is to hang out with her friend Colt at the park.
The && symbol is the logical AND operator, and it is used to combine two
logical expressions into one larger logical expression. If both smaller
expressions are true, then the entire expression evaluates to true. If either
one of the smaller expressions is false, then the whole logical expression
is false.
Another way to think about it is when the && operator is placed between the
two statements, the code literally reads, "if Colt is not busy AND the weather is
nice, then go to the park".
Logical expressions
Logical expressions are similar to mathematical expressions, except logical
expressions evaluate to either true or false.
11 != 12
Returns: true
You’ve already seen logical expressions when you write comparisons. A
comparison is just a simple logical expression.
Similar to mathematical expressions that use + , - , * , / and % , there are
logical operators && , || and ! that you can use to create more complex
logical expressions.
Logical operators
Logical operators can be used in conjunction with boolean values
( true and false ) to create complex logical expressions.
By combining two boolean values together with a logical operator, you create
a logical expression that returns another boolean value. Here’s a table
describing the different logical operators:
Operato Meanin
Example How it works
r g
value1
Logical Returns true if both value1 and value2 ev
&& &&
AND aluate to true .
value2
value1
Logical Returns true if either value1 or value2 (or
|| ||
OR even both!) evaluates to true .
value2
By using logical operators, you can create more complex conditionals like
Julia’s weekend example.
Here’s the logical expression used to represent Julia’s weekend plans:
var colt = "not busy";
The && symbol is the logical AND operator, and it is used to combine two
logical expressions into one larger logical expression. If both smaller
expressions are true, then the entire expression evaluates to true. If either
one of the smaller expressions is false, then the whole logical expression
is false.
Another way to think about it is when the && operator is placed between the
two statements, the code literally reads, "if Colt is not busy AND the weather is
nice, then go to the park".
Logical expressions
Logical expressions are similar to mathematical expressions, except logical
expressions evaluate to either true or false.
11 != 12
Returns: true
You’ve already seen logical expressions when you write comparisons. A
comparison is just a simple logical expression.
By combining two boolean values together with a logical operator, you create
a logical expression that returns another boolean value. Here’s a table
describing the different logical operators:
Operato Meanin
Example How it works
r g
value1
Logical Returns true if both value1 and value2 ev
&& &&
AND aluate to true .
value2
value1
Logical Returns true if either value1 or value2 (or
|| ||
OR even both!) evaluates to true .
value2
By using logical operators, you can create more complex conditionals like
Julia’s weekend example.
Before you advance any further in the lesson, here’s the truth tables for logical AND
( && ) and logical OR ( || ).
Truth Tables
&& (AND)
A B A && B
|| (OR)
A B A || B
Truth tables are used to represent the result of all the possible combinations of inputs
in a logical expression. A represents the boolean value on the left-side of the
expression and B represents the boolean value on the right-side of the expression.
Truth tables can be helpful for visualizing the different outcomes from a logical
expression. However, do you notice anything peculiar about the truth tables for logical
AND and OR?
Short-circuiting
For example, if you look at A AND B , if A is false, then regardless of the value B , the
total expression will always evaluate to false because both A and B must be true in
order for the entire expression to be true .
This behavior is called short-circuiting because it describes the event when later
arguments in a logical expression are not considered because the first argument
already satisfies the condition.
Quiz: Murder Mystery
LessonDownloads
Directions:
For this quiz, you're going to help solve a fictitious murder mystery(opens in a new
tab) that happened here at Udacity! A murder mystery is a game typically played at
parties wherein one of the partygoers is secretly, and unknowingly, playing a murderer,
and the other attendees must determine who among them is the criminal. It's a classic
case of whodunnit(opens in a new tab) .
Since this might be your first time playing a murder mystery, we've simplified things
quite a bit to make it easier. Here's what we know! In this murder mystery there are:
four rooms: the ballroom, gallery, billiards room, and dining room,
four weapons: poison, a trophy, a pool stick, and a knife,
and four suspects: Mr. Parkes, Ms. Van Cleve, Mrs. Sparr, and Mr. Kalehoff.
And we know that each suspect was located in a specific room at the time of the
murder.
Afterwards, use this template to print a message to the console if the mystery was
solved:
What goes into the three blank spaces? You can fill in the blanks with the name of the
suspect, the room, and the weapon! For example, an output string may look like:
node murder-mystery.js
If the case is not solved, you should get a message saying "The case is not solved!"
Expected Outcomes
Directions:
Using the flowchart below, write the code to represent checking your balance at the
ATM. The yellow diamonds represent conditional statements and the blue rectangles
with rounded corners represent what should be printed to the console.
Flowchart for checking your balance at the ATM (Click the image to enlarge the
flowchart).
Use the following variables in your solution:
balance - the account balance
isActive - if account is active
Hint: The variable balance could be a value less than, greater than, or equal to 0. The
variables isActive and checkBalance are booleans that can be set to true or false.
TIP: To print out the account balance with decimal points (i.e. 325.00), use
the .toFixed() method and pass it the number of decimal points you want to use. For
example, balance.toFixed(2) returns 325.00.
node balance.js
Expected Output
Directions:
Ice cream is one of the most versatile desserts on the planet because it can be done up
so many different ways and every ice cream shop offers a different set of flavors,
toppings and serving vessels.
Using logical operators, write a series of complex logical expressions that evaluates a
cusomer order and prints an approriate response.
node ice-cream.js
Expected Output
If you're like me, finding the right size t-shirt can sometimes be a challenge. What size
am I? What's the difference between S (small), M (medium), and L (large)? I usually
wear L, but what if I need an XL (extra large)?
Source: Teespring.com
Directions:
Use the sizing chart above, create a series of logical expressions that prints the size of
a t-shirt based on the measurements of shirtWidth , shirtLength , and shirtSleeve . Valid
sizes include S , M , L , XL , 2XL , and 3XL .
If shirtWidth , shirtLength , and shirtSleeve don't fit within the range of acceptable
values for a specific size, then print NA to the console. For example, if...
Then print N/A to the console because the measurements don't all match up with one
particular size .
node what-wear.js
Expected Output
18 28 8.13 S
Width Length Sleeve Size
20 29 8.38 M
22 30 8.63 L
24 31 8.88 XL
26 33 9.63 2XL
28 34 10.13 3XL
18 29 8.47 NA
In the rest of this lesson, we'll focus on some more advanced aspects of
working with conditional statements:
The paragraph above is pretty dense with information. You should probably re-read it
again! ☝️
Falsy values
A value is falsy if it converts to false when evaluated in a boolean context. For
example, an empty String "" is falsy because, "" evaluates to false . You already
know if...else statements, so let's use them to test the truthy-ness of "" .
if ("") {
} else {
That's right, there are only six falsy values in all of JavaScript!
Truthy values
A value is truthy if it converts to true when evaluated in a boolean context. For
example, the number 1 is truthy because, 1 evaluates to true . Let's use an if...else
statement again to test this out:
if (1) {
} else {
true
42
"pizza"
"0"
"null"
"undefined"
{}
[]
Essentially, if it's not in the list of falsy values, then it's truthy!
The paragraph above is pretty dense with information. You should probably re-read it
again! ☝️
Falsy values
A value is falsy if it converts to false when evaluated in a boolean context. For
example, an empty String "" is falsy because, "" evaluates to false . You already
know if...else statements, so let's use them to test the truthy-ness of "" .
if ("") {
} else {
That's right, there are only six falsy values in all of JavaScript!
Truthy values
A value is truthy if it converts to true when evaluated in a boolean context. For
example, the number 1 is truthy because, 1 evaluates to true . Let's use an if...else
statement again to test this out:
if (1) {
} else {
true
42
"pizza"
"0"
"null"
"undefined"
{}
[]
Essentially, if it's not in the list of falsy values, then it's truthy!
Open the developer console in your browser to test the output of options
mention in the above quiz.
Ternary Operator
LessonDownloads
Sometimes, you might find yourself with the following type of conditional.
const color;
if (isGoing) {
color = "green";
} else {
color = "red";
console.log(color);
Prints: "green"
In this example, the variable color is being assigned to either "green" or "red" based
on the value of isGoing . This code works, but it’s a rather lengthy way for assigning a
value to a variable. Thankfully, in JavaScript there’s another way.
TIP: Using if(isGoing) is the same as using if(isGoing === true) . Alternatively,
using if(!isGoing) is the same as using if(isGoing === false) .
Ternary operator
The ternary operator provides you with a shortcut alternative for writing lengthy if...else
statements.
To use the ternary operator, first provide a conditional statement on the left-side of
the ? . Then, between the ? and : write the code that would run if the condition
is true and on the right-hand side of the : write the code that would run if the condition
is false . For example, you can rewrite the example code above as:
console.log(color);
Prints: "green"
This code not only replaces the conditional, but it also handles the variable assignment
for color .
If you breakdown the code, the condition isGoing is placed on the left side of the ? .
Then, the first expression, after the ? , is what will be run if the condition is true and the
second expression after the, : , is what will be run if the condition is false.
Example
Here's a comparison of using an if...else statement vs. using a ternary operator.
Show TranscriptSummarize Video
Note - This video does not have an audio. It was created as a visual to aid
learning.
From the smallest of creatures to the largest of animals, inevitably every living,
breathing thing must ingest other organisms to survive. This means that all animals will
fall within one of the three consumer-based categories based on the types of food that
they eat.
Directions:
Write a series of nested ternary statements that sets the variable category equal to:
HINT If you get stuck, try drawing a diagram of the decision tree.
Running Your Code
Create a file named food-chain.js , implement the solution, and run the command below
on your terminal:
node food-chain.js
Expected Output
If you find yourself repeating else if statements in your code, where each condition is
based on the same value, like this:
if (option === 1) {
Switch Statement
A switch statement is an another way to chain multiple else if statements that are
based on the same value without using conditional statements. Instead, you
just switch which piece of code is executed based on a value.
switch (option) {
case 1:
case 2:
case 3:
case 4:
console.log("You selected option 4.");
case 5:
case 6:
Here, each else if statement ( option === [value] ) has been replaced with
a case clause ( case [value]: ) and those clauses have been wrapped inside the switch
statement.
When it finds a match, it transfers control to that case clause to executed the code for
that case.
const option = 3;
switch (option) {
...
Prints:
You selected option 3.
You selected option 4.
You selected option 5.
You selected option 6.
...then the switch statement prints out options 3, 4, 5, and 6.
But that’s not exactly like the original if...else code at the top? So what’s missing?
We can prevent the code from falling through by adding a break statement at the end
of each case.
The break statement will terminate the switch statement and transfer control to the
code following the switch statement which prevents the switch statement from falling
through and running the code in the other case clauses.
const option = 3;
switch (option) {
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
switch (option) {
case 1:
break;
case 2:
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
default:
switch(favoriteFood) {
case "pizza":
restaurant ="pizzeria";
break;
default:
restaurant ="diner";
break;
case "tacos":
restaurant ="taqueria";
break;
case "sushi":
restaurant ="sushi bar";
break;
case "pancakes":
restaurant ="pancake house";
break;
}
const month = 7;
let days;
switch(month) {
case 1:
case 2:
days = 28;
break;
case 3:
case 4:
days = 30;
break;
case 5:
case 6:
days = 30;
break;
case 7:
case 8:
case 9:
days = 30;
break;
case 10:
case 11:
days = 30;
break;
case 12:
default:
days = 31;
}
const month = 5;
let days;
switch (month) {
case 1:
days = 31;
break;
case 2:
days = 28;
break;
case 3:
days = 31;
break;
case 4:
days = 30;
break;
case 5:
days = 31;
break;
case 6:
days = 30;
break;
case 7:
days = 31;
break;
case 8:
days = 31;
break;
case 9:
days = 30;
break;
case 10:
days = 31;
break;
case 11:
days = 30;
break;
case 12:
days = 31;
}
var month = 2;
switch(month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
days = 28;
}
console.log("There are " + days + " days in this
month.");
In some situations, you might want to leverage the "falling-through" behavior of switch
statements to your advantage.
switch (tier) {
default:
console.log(output);
Prints: You’ll receive one copy of the NSFW (Not Safe for Work) Exploding Kittens card
game and one copy of the Exploding Kittens card game.
In this example, based on the successful Exploding Kittens Kickstarter
campaign(opens in a new tab) (a hilarious card game created by Elan Lee), each
successive tier builds on the next by adding more to the output. Without any break
statements in the code, after the switch statement jumps to the "nsfw deck" , it
continues to fall-through until reaching the end of the switch statement.
switch (tier) {
...
default:
console.log(output);
Prints: You’ll receive one copy of the Exploding Kittens card game.
By using the falling-through behavior of switch statements, you can represent
hierarchical-type scenarios like the Kickstarter backer program.
If winner is equal to 3, then what will be output to the console?
switch (winner) {
case 1:
prize += "a trip for two to the Bahamas and ";
case 2:
prize += "a four piece furniture set.";
break;
case 3:
prize += "a smartwatch and ";
default:
prize += "tickets to the circus.";
}
NOTE: Wondering what the average salary would be for a person with a Nanodegree
from Udacity? That's a hard question to answer, but that doesn't mean we haven't tried
to quantify the value of our Nanodegrees. Read more about Nanodegrees from
resident Udacity writer, Chris Watkins, here(opens in a new tab)
Directions:
Write a switch statement to set the average salary of a person based on their type of
completed education.
TIP: To print out the average salary with commas (i.e. 59,124), use
the toLocaleString() method and pass it the locale "en-US". For
example, salary.toLocaleString("en-US") .
node back-to-school.js
Salary Education
Great work!
Loops Repeat Blocks of Code
Conditional statements are one way to control the flow of code -- if a certain condition is
true, execute this block of code, otherwise, execute that other block of code.
Loops are another way to control the flow of code by allowing us to execute a block of
code multiple times.
Along the way we'll give you a lot of practice writing loops.
let x = 1;
x = x + 1;
x = x + 1;
.
.
Prints:
1 missippi!
2 missippi!
3 missippi!
and so on...
This works, but it is very time consuming. There has to be a better way!
let x = 1;
while (x <= 10000) {
console.log(x + " mississippi!");
x = x + 1;
}
Why does the video use var and the example code
use let ?
Although var works, it is now considered best practice to use let or const instead.
We're showing you both but you should try to use let and const in your own code.
Three main pieces of information that any loop should have are:
1. When to start: The code that sets up the loop — defining the starting value of
a variable for instance.
2. When to stop: The logical condition to test whether the loop should continue.
3. How to get to the next item: The incrementing or decrementing step — for
example, x = x * 3 or x = x - 1
Here's a basic while loop example that includes all three parts.
console.log(“start = ”, start);
Prints:
start = 0
start = 2
start = 4
start = 6
start = 8
If a loop is missing any of these three things, then you might find yourself in trouble. For
instance, a missing stop condition can result in a loop that never ends!
while (true) {
If you did try to run that code in the console, you would probably crash your browser
tab.
WARNING: You’re probably reading this because you didn't heed our warnings about
running that infinite loop in the console. If your browser tab has crashed or has become
frozen/unresponsive, there are a couple ways to fix this. If you are using Firefox, the
browser will popup a notification about your script being unresponsive, and will give you
the option to kill the script (do that). If you're using Chrome, go to the taskbar and select
Window > Task Manager. You can end the process for the particular tab you ran the
script in through the task manager. If you’re not using Firefox or Chrome, download
Firefox or Chrome ;).
An infinite loop will run forever... until your browser crashes or you stop this video 😉
Note - This video does not have an audio. It was created as a visual to aid learning.
Here's an example where a loop is missing how to get to the next item; the variable x is never
incremented. x will remain 0 throughout the program, so the loop will never end.
let x = 0;
while (x < 1) {
This code will also crash your browser tab, so we don't recommend running it.
Question 1 of 2
How many times will the while loop run?
let x = 10;
while (x <= 25) {
console.log('Printing out x = ' + x);
x = x + 2;
}
15
Question 2 of 2
Here's a while loop that is supposed to print out the values of x from 0 to 5, but
there's a bug. What is missing? Hint: there may be more than one correct
response.
while (x < 6) {
console.log('Printing out x = ' + x);
}
Quiz: JuliaJames
LessonDownloads
Directions:
Write a while loop that:
node julia-james.js
Node.js(opens in a new tab) is an open-source backend JavaScript runtime
environment. That basically means that it will run your JavaScript code in the terminal
and return any output that is generated. We'll be using Node to run the code in our
workspaces for many of the quizzes in this course.
1
2
Julia
4
James
Julia
7
8
Julia
James
11
Julia
13
14
JuliaJames
16
17
Julia
19
James
⚠️These workspaces will autosave your code -- but it can take a few seconds.
If you try to run your code before the autosave occurs, you might see an error like this:
ReferenceError: Cannot access 'variableName' before initialization
Wait a few seconds and try to run the code again.
Try the quiz on your own first. If you get stuck you can check the solution by clicking the
button below.
Show Solution
Directions:
Write a loop that prints out the following song. Starting at 99, and ending at 1 bottle.
...
It may not seem critical in this code, but what if you were coding a message to be sent
out to important customers? Using incorrect pluralization might cause them to question
your organization's competence and ability to meet their needs.
node juice.js
Your code should print out the song lyrics above. Double check the last three links to
make sure you use the correct pluralization of "bottles" for each verse of the song.
Directions:
Write a while loop that counts down from 60 seconds and:
If there's a task being completed, it prints out the task
If there is no task being completed, it prints out the time as T-x seconds
node liftoff.js
Your output should look like the following:
T-60 seconds
T-59 seconds
T-58 seconds
...
T-51 seconds
Orbiter transfers from ground to internal power
T-49 seconds
...
T-3 seconds
T-2 seconds
T-1 seconds
Solid rocket booster ignition and liftoff!
Loop Basics
A loop should always include:
Where to start
When to stop
How to get to the next item
It's easy to forget some of these pieces in a while loop and end up with an
infinte loop that crashes your browser!
for loops give you more control over the looping process.
for Loops Require a Start, Stop and Step
The for loop explicitly forces you to define the start point, stop point, and
each step of the loop. In fact, you'll get an Uncaught SyntaxError:
Unexpected token ) if you leave out any of the three required pieces.
// do this thing
Here's an example of a for loop that prints out the values from 0 to 5. Notice
the semicolons separating the different statements of the for loop: var i = 0; i
< 6; i = i + 1
Prints:
Printing out i = 0
Printing out i = 1
Printing out i = 2
Printing out i = 3
Printing out i = 4
Printing out i = 5
6 false
0 true 0 true 0, 0
0 true 1 true 0, 1
0 true 2 false
1 true 0 true 1, 0
1 true 1 true 1, 1
1 true 2 false
2 true 0 true 2, 0
2 true 1 true 2, 1
2 true 2 false
3 false
Try It Yourself!
Paste this nested loop in your browser and take a look at what it prints out:
for (let x = 0; x < 5; x = x + 1) {
Prints:
0, 0
0, 1
0, 2
1, 0
1, 1
1, 2
2, 0
2, 1
2, 2
3, 0
3, 1
3, 2
4, 0
4, 1
4, 2
Notice the order that the output is being displayed.
For each value of x in the outer loop, the inner for loop executes completely. The outer loop
starts with x = 0 , and then the inner loop completes its cycle with all values of y :
Once the inner loop is done iterating over y , then the outer loop continues to the next value, x
=1 , and the whole process begins again.
etc.
NOTE: Nested loops can be tricky at first. We will revisit them again when we talk
about arrays, which are lists of data. Loops are very helpful when working with arrays.
Quiz Question
What will this loop print out?
123456
Increment Operator
The increment operator ++ adds one a to variable, returns a value and assigns the
incremented value to the variable.
x++ is the postfix operator, which means that it returns the value before incrementing
it:
let x = 2;
++x is the prefix operator, which means that it returns the value after incrementing it:
let x = 2;
++x // assigns 31 as the value of x then returns 3
Decrement Operator
a The decrement operator -- subtracts one from a variable, returns a value and
assigns the decremented value to the variable.
Similiar to the increment operator, x-- is the postfix operator, which means that it
returns the value before incrementing it:
let x = 2;
--x is the prefix operator, which means that it returns the value after incrementing it:
let x = 2;
Assignment Operators
An assignment operator is a shorthand way to peform a mathematical operation on a
variable and assigns that value to the variable.
You can use assignment operators for addition, subtraction, multiplication, and division.
// Add y to x
x += y // x = x + y
// Subtract y from x
x -= y // x = x - y
// Multiply x by x
x *= y // x = x* y
// Divide x by y
x /= y // x = x / y
These assignment operators will come in handy as you create more loops!
What does this code log out? javascript let x = 4; x++; console.log(x);
Directions:
Rewrite the following while loop as a for loop:
let x = 9;
while (x >= 1) {
console.log("hello " + x);
x = x - 1;
}
hello 9
hello 8
hello 7
hello 6
hello 5
hello 4
hello 3
hello 2
hello 1
Directions:
Here is a for loop that's supposed to print the numbers 5 through 9. Fix the errors!
console.log(x);
Your Code:
node fix1.js
Testing Your Code
Make sure you are using a for loop and your code runs without errors. You should get
this output:
5
6
7
8
9
Directions:
The for loop below has an error. Fix it!
console.log(k);
Your Code:
node fix2.js
12
13
14
15
16
17
18
19
20
Directions:
Write a for (note: not a function) loop that prints out the factorial of the number 12:
A factorial is calculated by multiplying a number by all the numbers below it. For
instance, 3! or "3 factorial" is 3 \ *2 * 1 = 6
3!=3∗2∗1=63!=3∗2∗1=6
4!=4∗3∗2∗1=244!=4∗3∗2∗1=24
5!=5∗4∗3∗2∗1=1205!=5∗4∗3∗2∗1=120
Save your final answer in a variable called solution and print it to the console.
node factorials.js
479001600
Directions:
Example output for row-seat information: output each row and seat number on a
separate line
0-0
0-1
0-2
...
25-97
25-98
25-99
Functions are very helpful as the problems we need to solve with our code get more
complex. We'll often need to repeat steps on different inputs -- and functions let us do
that!
function reverseString(reverseMe) {
reversed += reverseMe[i];
}
return reversed;
console.log(reverseString("Julia"));
Prints "ailuJ"
Let's break it down:
Annotated Function
// Set one parameter to hold the value of the input
string
function reverseString(reverseMe) {
reversed += reverseMe[i];
return reversed;
console.log(reverseString("Julia"));
Using a function simplifies the process and allows you to reuse the function by calling it
by its name and passing it in a string. If these steps were not wrapped inside
this reverseString function you would to write all of this code each time you needed to
reverse a string.
Free Response
Why does the video use var and the example code
use const ?
We'll talk about var , let , and const a bit later in this lesson when we
discuss scope!
How to Declare a Function
Functions allow you to package up lines of code that you can use (and often reuse) in
your programs.
Sometimes they take parameters like the pizza button from the beginning of this
lesson. reheatPizza() had one parameter: the number of slices.
function reheatPizza(numSlices) {
The reverseString() function that you saw also had one parameter: the string to be
reversed.
function reverseString(reverseMe) {
In both cases, the parameter is listed as a variable after the function name, inside the
parentheses. And, if there were multiple parameters, you would just separate them with
commas.
But, you can also have functions that don't have any parameters. Instead, they just
package up some code and perform some task. In this case, you would just leave the
parentheses empty. Take this one for example. Here's a simple function that just prints
out "Hello!" .
function sayHello() {
const message = "Hello!"
console.log(message);
If you tried pasting any of the functions above into the JavaScript console, you probably
didn't notice much happen. In fact, you probably saw undefined returned back to
you. undefined is the default return value on the console when nothing
is explicitly returned using the special return keyword.
Return Statements
In the sayHello() function above, a value is printed to the console with console.log ,
but not explicitly returned with a return statement. You can write a return statement by
using the return keyword followed by the expression or value that you want to return.
function sayHello() {
function sayHello() {
const message = "Hello!"
console.log(sayHello());
Prints: "Hello!"
Question 1 of 2
Use the following function to answer this question.
function findAverage(x, y) {
const answer = (x + y) / 2;
return answer;
}
function add(x, y) {
// function body
const sum = x + y;
// function body!
return sum;
add(1, 2);
Returns: 3
Directions:
1. Declare a function called laugh() that returns "hahahahahahahahahaha!" .
2. Print the value returned from the laugh() function to the console. It should
print:
hahahahahahahahahaha!
node laugh1.js
Node.js(opens in a new tab) is an open-source backend JavaScript runtime
environment. That basically means that it will run your JavaScript code in the terminal
and return any output that is generated. We'll be using Node to run the code in our
workspaces for many of the quizzes in this course.
⚠️These workspaces will autosave your code -- but it can take a few seconds.
If you try to run your code before the autosave occurs, you might see an error like this:
ReferenceError: Cannot access 'variableName' before initialization
console.log(laugh(3));
Prints: "hahaha!"
node laugh2.js
hahaha!
hahahaha!
hahahahahahahaha!
Don't worry if your guess is wrong! We'll explore what is happening with logging and
return values in the video.
Question 1 of 5
What do you think will be printed when this code is run?
function whatHappens() {
console.log("I am printing to the console");
return "I am returning a value";
}
whatHappens();
Question 2 of 5
What do you think will be printed when this code is run?
Notice that in this code we are wrapping our function call
in console.log statement.
function whatHappens() {
console.log("I am printing to the console");
return "I am returning a value";
}
console.log(whatHappens());
Using console.log to test your code in the JavaScript console or to print out values as
your code runs can be extremely helpful in pinpointing where something has gone
wrong in your code.
Math Review:
A prime number is an integer that is not a product of two smaller integers.
The modulo operator(opens in a new tab) % returns the remainder left over when
one operand is divided by a second operand.
You can check for prime numbers by dividing them by smaller integers. If the number
can be divided without remainder by any integer greater than 1 it is not a prime number.
function isPrime(integer) {
for (let x = 2; x < integer; x++ ) {
if(integer % x === 0) {
console.log(integer + " is divisible by " +
x);
return false
}
}
return true
}
Exploring console.log and return Statements
Paste the following function declaration and function invocation into the JavaScript
console to see the difference between logging (printing) and returning:
function isThisWorking(input) {
isThisWorking(3);
function isThisWorking(input) {
isThisWorking(3);
Question 3 of 5
What does this function "return"?
function sleep() {
console.log("I'm sleepy!");
return "zzz";
return "snore";
}
sleep();
"I'm sleepy!"
"zzz"
"snore"
"zzz"
"snore"
Submit
Question 4 of 5
What number will be printed (to the JavaScript console)?
function square(x) {
return x * x;
}
function subtractFour(x) {
return square(x) - 4;
}
console.log(subtractFour(5));
25
21
5
Submit
Question 5 of 5
What do you think will happen with the following code?
function test() {
return 1;
return 2;
}
test();
1 will be returned
2 will be returned
3 will be returned
error
Using Return Values
Returning a value from a function is great, but what's the use of a return value if you're
not going to use the value to do something?
A function's return value can be stored in a variable or reused throughout your program
as a function argument.
Example
Here, we have a function that adds two numbers together, and another function that
divides a number by 2. We can find the average of 5 and 7 by using the add() function
to add a pair of numbers together, and then by passing the sum of the two
numbers add(5, 7) into the function divideByTwo() as an argument.
And finally, we can even store the final answer in a variable called average and use the
variable to perform even more calculations in more places!
Quiz Question
Try predicting what will be printed in the console.log statement below. Then,
check your prediction by pasting the code into the JavaScript console. Functions
can be tricky, so try figuring it out before running the code!
function addTen(x) {
return x + 10;
}
function divideByThree(y) {
return y / 3;
}
Understanding Scope
Scope is the part of the program where a particular identifier, such as a
variable or a function name, is visible or accessible.
When James is inside the library he is in scope to find out where the
book Great Expectations is shelved. The librarian has that information is able to
share it with James.
When James is outside of the library, the person he asked about Great
Expectations didn't have any information about library or the books inside of it.
In this case, James's question is out of scope.
The Three Types of Scope in JavaScript
JavaScript has three types of scope.
global scope
function scope
block scope
Variables declared in the global scope are accessible to any other part of the
program.
Variables declared inside a function are in the function scope which means
they are only accessible inside that function.
Block scope is more complicated. We'll learn more about that later in this lesson.
function library() {
function classicLiterature() {
classicLiterature();
Global Scope
bookSeeker and book are in the global scope so as expected, we can use access
them anywhere in the code.
library();
Prints
James asked Julia for Great Expectations
James found Great Expectations on the Dickens shelf!
Function Scope
What happens when we try to access the librarian variable declared inside
the library function from the console? We get an error, because librarian can only
be accessed inside the scope of the library function.
console.log(librarian);
console.log(shelf);
Returns Uncaught ReferenceError: shelf is not defined
Question 1 of 4
Which of these variables a , b , c , and d , are defined in the global scope?
const a = 1;
function x() {
const b = 2;
function y() {
const c = 3;
function z() {
const d = 4;
}
z();
}
y();
}
x();
c
d
Submit
Question 2 of 4
Which of these variables a , b , c , and d , are defined in the scope of
function x ?
const a = 1;
function x() {
const b = 2;
function y() {
const c = 3;
function z() {
const d = 4;
}
z();
}
y();
}
x();
a
b
d
Submit
Question 3 of 4
Which of these variables a , b , c , and d , are defined in the scope of
function y ?
const a = 1;
function x() {
const b = 2;
function y() {
const c = 3;
function z() {
const d = 4;
}
z();
}
y();
}
x();
a
d
Submit
Question 4 of 4
Which of these variables a , b , c , and d , are defined in the scope of
function z ?
const a = 1;
function x() {
const b = 2;
function y() {
const c = 3;
function z() {
const d = 4;
}
z();
}
y();
}
x();
d
Submit
1. The JavaScript engine will start looking in the scope where the variable is
requested.
2. If it can't find it in the current scope, it moves out one level and checks again.
3. It keeps moving to the outer scope until it has reached the global scope.
4. If the JavaScript engine checks all of the outer functions and global scope,
and it still can't find the identifier then it will return a Reference error.
// <-- 4. JavaScript engine looks here last
const globalVar = "I am in the global scope";
function outerOuterFunction() {
// <-- 3. JavaScript engine looks here third
const outerOuterVar = 'I am in the outerOuterFunction scope';
function outerFunction() {
// <-- 2. JavaScript engine looks here second
const outerVar = 'I am in the outerFunction scope';
function innerFunction() {
// <-- 1. JavaScript engine looks here first
const innerVar = 'I am in the innerFunction scope';
console.log(globarVar);
}
}
}
uestion 2 of 2
Where can you print out the value of variable d without an error?
const a = 1;
function x() {
const b = 2;
function y() {
const c = 3;
function z() {
const d = 4;
}
z();
}
y();
}
x();
What is a Block?
A block is a group of statements in between curly braces. You've seen blocks in conditional
statements:
const x = 5;
if (x < 6) {
const double = x * 2;
console.log(double);
} else {
const half = x / 2;
console.log(half);
and in loops:
let triple = x * 3;
console.log(triple);
In the examples above, the variables double , half and triple are only available inside the
block where they are declared.
Question 1 of 4
What will print out to the console when this code is run?
banana
apple
banana
banana
Question 2 of 4
What will print out to the console when this code is run?
apple
apple
banana
apple
if (true) {
var x = "x is accessible";
let y = 'y is accessible';
const z = 'z is accessible';
console.log("Inside the if block scope:");
console.log(x);
console.log(y);
console.log(z);
}
console.log("Outside the if block scope:")
console.log(x);
console.log(y);
console.log(z);
What will print out when you run this code?
function whereAmIAssessible(a) {
if (a) {
var x = "x is accessible";
let y = 'y is accessible';
const z = 'z is accessible';
console.log("Inside the if block scope:");
console.log(x);
console.log(y);
console.log(z);
}
}
whereAmIAssessible(true);
console.log("Outside the function scope:")
console.log(x);
console.log(y);
console.log(z);
"Why wouldn't I always use global variables? Then, I would never need to use function
arguments since ALL my functions would have access to EVERYTHING!"
Well... Global variables might seem like a convenient idea at first, especially
when you're writing small scripts and programs, but there are many reasons
why you shouldn't use them unless you have to. For instance, global variables
can conflict with other global variables of the same name. Once your
programs get larger and larger, it'll get harder and harder to keep track and
prevent this from happening.
There are also other reasons you'll learn more about in more advanced
courses. But for now, just work on minimizing the use of global variables as
much as possible.
Shadowing occurs when variables in different scopes have the same name. When this
happens the variable in the inner scope overrides the variable in the outer scope.
console.log(bookTitle);
function displayBookEnglish() {
console.log(bookTitle);
displayBookEnglish()
console.log(bookTitle);
Prints
Le Petit Prince
The Little Prince
The Little Prince
Example: no shadowing
console.log(bookTitle);
function displayBookEnglish() {
console.log(bookTitle);
displayBookEnglish()
console.log(bookTitle);
Prints
Le Petit Prince
The Little Prince
Le Petit Prince
Question 1 of 2
Without pasting into your console, what do you think this code will print out?
let x = 1;
function addTwo() {
x = x + 2;
}
addTwo();
x = x + 1;
console.log(x);
4
Submit
Question 2 of 2
Without pasting into your console, what do you think this code will print out?
var x = 1;
function addTwo() {
var x = x + 2;
}
addTwo();
x = x + 1;
console.log(x);
4
Submit
You may be wondering... Why are we using var in the second quiz?
Great question! There are some interesting implications of block scoping that we will
cover later in the lesson. If you want a preview, try replacing var with let and see
what happens when you run the code. We'll explore this more when we look into
hoisting.
}
return catMessage;
};
// code here
};
Returns:
function(max) {
let catMessage = ""
for (let i = 0; i < max; i++) {
catMessage += "meow ";
}
return catMessage;
}
Hoisting
Sometimes your JavaScript code will produce errors that may seem
counterintuitive at first. Hoisting is another one of those topics that might be
the cause of some of these tricky errors you're debugging.
JavaScript is Different!
In most programming languages, you have to declare a function or variable before you
can call it.
Intuitively, you might think "This code shouldn't work!" because we're trying to
call findAverage before it is declared:
findAverage(5, 9);
function findAverage(x, y) {
var answer = (x + y) / 2;
return answer;
}
Returns 7
in JavaScript we can call a function before we declare it due to hoisting. Before any
JavaScript code is executed, all function declarations and variables declared
with var are hoisted to the top of their current scope.
So even though the code you write doesn't change, the JavaScript engine interprets it
as if you wrote it this way:
function findAverage(x, y) {
var answer = (x + y) / 2;
return answer;
}
findAverage(5, 9);
Demo Code
Hosting can lead to odd results.
function sayGreeting() {
console.log(greeting);
sayGreeting()
function sayGreeting() {
console.log(greeting);
var greeting;
}
sayGreeting()
Returns undefined
We don't get an error, but we do get undefined . Can we fix this by assigning a value
to greeting when we declare it? No. This code still returns undefined :
function sayGreeting() {
console.log(greeting);
sayGreeting()
Returns undefined
This is because with hoisting, the variable declaration is being hoisted to the top of the
function, but the value of greeting isn't assigned until after the console.log statement
is run. JavaScript is interpreting the code as if it were written like this:
function sayGreeting() {
var greeting;
console.log(greeting);
greeting = "hello";
sayGreeting()
Returns undefined
To avoid bugs like this, declare your functions at the top of your scripts and declare and
assign your variables at the top of your functions.
function sayGreeting() {
console.log(greeting);
greeting
}
sayGreeting()
Prints Hello
Question 1 of 3
What value will be printed to the console?
sayHi("Julia");
function sayHi(name) {
console.log(greeting + " " + name);
var greeting;
}
Uncaught ReferenceError
undefined
Julia
undefined Julia
null
null Julia
NaN
NaN Julia
Hello Julia
Submit
Question 2 of 3
What value will be printed to the console?
sayHi("Julia");
function sayHi(name) {
console.log(greeting + " " + name);
var greeting = "Hello";
}
Uncaught ReferenceError
undefined
Julia
undefined Julia
null
null Julia
NaN
NaN Julia
Hello Julia
Submit
Question 3 of 3
What value will be printed to the console?
function sayHi(name) {
var greeting = "Hello";
console.log(greeting + " " + name);
}
sayHi("Julia");
Uncaught ReferenceError
undefined
Julia
undefined Julia
null
null Julia
NaN
NaN Julia
Hello Julia
console.log(meow(2));
return catMessage;
};
function purr() {
return 'purrrr!';
cat();
function cat() {
console.log(purr());
return catMessage;
};
function purr() {
return 'purrrr!';
cat();
Prints purrrr!
When a variable is declared using let or const inside a block of code (denoted by
curly braces { } ), the variable is stuck in the temporal dead zone until the variable’s
declaration is processed. This sounds scary, but it basically means that the code cannot
access the variable before it has been declared. If you try, you'll get a Reference Error.
Question 1 of 3
What will happen when you run this code?
function getClothing(isCold) {
if (isCold) {
var freezing = 'Grab a jacket!';
} else {
var hot = 'It’s a shorts kind of day.';
console.log(freezing);
}
}
getClothing(false)
Grab a jacket!
undefined
Question 2 of 3
What will happen when you run this code?
function getClothing(isCold) {
if (isCold) {
const freezing = 'Grab a jacket!';
} else {
const hot = 'It’s a shorts kind of day.';
console.log(freezing);
}
}
getClothing(false)
Grab a jacket!
undefined
Question 3 of 3
What will be returned when you run this code?
let x = 1;
function addTwo() {
let x = x + 2;
}
addTwo();
x = x + 1;
console.log(x);
2
3
Hoisting Recap
LessonDownloads
Best Practices
Declare functions and variables at the top of your scripts, so the syntax and
behavior are consistent with each other.
Use let and const to declare variables. You may see var in legacy code,
but do not use it in any new code you are writing.
Build a Triangle
LessonDownloads
Directions:
For this quiz, you're going to create a function called buildTriangle() that will accept
an input (the triangle at its widest width) and will return the string representation of a
triangle. See the example output below.
buildTriangle(10);
Returns:
* *
***
*** *
*** * *
*** ***
*** *** *
*** *** * *
* * * * * * * * * *
We've given you one function makeLine() to start with. The function takes in a line
length, and builds a line of asterisks and returns the line with a newline character.
function makeLine(length) {
}
return line + "\n";
Think It Through!
This will be the most complicated program you've written yet, so take some
time thinking through the problem before diving into the code. What tools will you need
from your JavaScript tool belt? Professionals plan out their code before writing anything.
Think through the steps your code will need to take and write them down in order. Then
go through your list and convert each step into actual code. Good luck!
node triangle.js
* *
***
If you try with console.log(buildTriangle(6)); the following should be logged into the
console:
* *
** *
*** *
*** * *
*** ***
*
* *
** *
*** *
*** * *
*** ***
*** *** *
*** *** * *
* * * * * * * * *
* * * * * * * * * *
Functions as Parameters
Being able to store a function in a variable makes it really simple to pass the function
into another function. A function that is passed into another function is called
a callback. Let's say you had a helloCat() function, and you wanted it to return
"Hello" followed by a string of "meows" like you had with catSays . Well, rather than
redoing all of your hard work, you can make helloCat() accept a callback function,
and pass in catSays .
return catMessage;
};
function helloCat(callbackFunc) {
helloCat(catSays);
You can create a function expression with a named function, like this:
};
But you still need to use its assigned identifier to call it:
favoriteMovie();
movie();
Returns Uncaught ReferenceError: movie is not defined
messageFunction(name);
// Call the movies function, pass in the favoriteMovie function and name of
movie
messageFunction(name);
// Call the movies function, pass in the function and name of movie
movies(function displayFavorite(movieName) {
}, "Finding Nemo");
Anonymous inline function expressions are often used with function callbacks that are
unlikely to be reused elsewhere. Yes, you could store the function in a variable, give it a
name, and pass it in like you saw in the examples above. However, when you know the
function is not going to be reused, it could save you many lines of code to just define it
inline.
For example, we can make the callback function in movies an anonymous inline
function like this:
return y + 1;
};
return y + 1;
};
// for either of the definitions above, call the function like this:
doSomething(5);
Returns: 6
You can even pass a function into another function inline. This pattern is
commonly used in JavaScript, and can be helpful streamlining your code.
// function declaration that takes in two arguments: a function for
displaying
// a message, along with a name of a movie
function movies(messageFunction, name) {
messageFunction(name);
}
// call the movies function, pass in the function and name of movie
movies(function displayFavorite(movieName) {
console.log("My favorite movie is " + movieName);
}, "Finding Nemo");
Laugh
LessonDownloads
Directions:
Write an anonymous function expression that:
Returns: hahaha!
node laugh3.js
ha!
hahahahaha!
hahahahahahahahahaha!
Directions:
Write a named function expression that stores the function in a variable called cry and
returns "boohoo!". Don't forget to call the function using the variable name, not the
function name:
cry();
Returns: boohoo!
Your Code:
node cry.js
boohoo!
Directions:
Call the emotions() function so that it prints the output you see below, but instead of
passing the laugh() function as an argument, pass an inline function expression
instead.
node inline.js
0 I am happy, !
2 I am happy, haha!
7 I am happy, hahahahahahaha!
And we'll be talking a lot about donuts and how to keep track of all of the
donuts in our shop!
Sometimes it is easier to view an array with one item on each line, like this:
"chocolate frosted",
"cinnamon",
"sprinkled",
"powdered",
"cinnamon sugar",
"glazed cruller",
"chocolate cruller",
"cookies",
"Boston creme",
"creme de leche",
Great question! While you can use var to declare an array, it is now considered best
practice to use let or const instead. We're showing you both but you should try to
use let and const in your own code.
We'll learn more about why we use const after we learn how to access array elements
using the index.
Arrays
An array is useful because it stores multiple values into a single, organized data
structure. You can define a new array by listing values separated with commas between
square brackets [] .
But strings aren’t the only type of data you can store in an array. You can also store
numbers, booleans… and really anything!
Nested arrays can be particularly hard to read, so it's common to write each nested
array on one line, using a newline after each comma:
const arraysInArrays = [
[1, 2, 3],
["Julia", "James"],
[true, false, true, false]
];
Later in this lesson, we’ll look into some unique situations where nested arrays can be
useful.
This syntax is valid, but it is preferred to use the literal constructor because the syntax is
simpler and more intuitive.
Quiz Question
Select the valid arrays from the list below.
const donuts = [
"glazed", // index is 0
"chocolate frosted", // index is 1
"cinnamon", // index is 2
"sprinkled", // index is 3
"powdered", // index is 4
"cinnamon sugar", // index is 5
"glazed cruller", // index is 6
"chocolate cruller", // index is 7
"cookies", // index is 8
"Boston creme", // index is 9
"powdered jelly filled", // index is 10
"creme de leche", // index is 11
"glazed donut holes", // index is 12
"blueberry donut holes", // index is 13
"cake donut holes", // index is 14
"chocolate donut holes" // index is 15
];
console.log(donuts[11]); // "creme de leche"
Prints: "glazed"
Prints: undefined
Avoid accessing elements outside the bounds of an array. If you try to, the missing
element will be returned back as undefined !
Question 1 of 3
Take a look at the following donuts array.
What line of code would you use to select the "coconut" donut from
the donuts array?
donuts[0]
donuts[4]
donuts[6]
donuts[7]
Submit
console.log(donuts[1]);
Question 2 of 3
We’ve decided to replace some of donuts in the donuts array.
What does the donuts array look like after the following changes?
A simple way to think about the difference between let and const is that we
use let when we anticipate that the value of a variable will change and const when
we think it will be constant -- but that shorthand doesn't tell the whole story. The
difference between let and const is not so much about change but
about reassignment
The decision about whether we need to be able to reassign the variable is based on
what type of variable it is and how JavaScript stores those values.
Primitive vs. Object Types in JavaScript
String, Number, Boolean, undefined and null are considered Primitive
Types(opens in a new tab) in JavaScript. These relatively simple data types represent
just one value which makes it easy for JavaScript to store that value. So when you
assign a primitive value to a variable, JavaScript actually assigns that value.
Arrays are more complicated because they consist of a list of values which makes
storage much more complicated. Arrays are actually Object types(opens in a new
tab) which means that instead of assigning all of the values of the list to the array,
JavaScript simply assigns a reference to where to find the values in the array. Even if
the values inside the object change, the reference address doesn't.
Here's an analogy that might help. Think of a JavaScript array as if it were a house. The
house has a group of people who live inside it. If those people move out, and a new
group of people moves in, the names of the people inside the house changes, but the
house's postal address won't.
Question 3 of 3
Which of these code snippets will throw an error?
const myVar = 2;
myVar = 4;
let myVar = 2;
let myVar = 4;
let myVar = 2;
myVar = ["banana", "apple"];
TLDR;
We use const to declare arrays because JavaScript is assigning a reference that
points to that array. We can change whatever we want inside the array, but we cannot
change which array the variable points to.
Directions:
Create an array called udaciFamily and add "Julia", "James", and your name to the
array. Then, print the udaciFamily to the console using console.log .
node udacifamily.js
Node.js(opens in a new tab) is an open-source backend JavaScript runtime
environment. That basically means that it will run your JavaScript code in the terminal
and return any output that is generated. We'll be using Node to run the code in our
workspaces for many of the quizzes in this course.
When your code runs, you should get this output:
Julia
James
Yourname
⚠️These workspaces will autosave your code -- but it can take a few seconds.
If you try to run your code before the autosave occurs, you will see an error like this:
ReferenceError: Cannot access 'variableName' before initialization
Wait a few seconds and try to run the code again.
The space western TV drama Firefly(opens in a new tab) premiered in the United
States on September 20, 2002. Although the show only featured fourteen episodes and
was canceled during its first season, it continues to remain popular today with a growing
fan base. In the show, the captain Mal(opens in a new tab) , played by Nathan
Fillion(opens in a new tab) , leads a crew of renegades on the spaceship Serenity.
Directions:
Create an array called crew to organize the Serenity’s crew and set it equal to the
variables below . You don't need to type out the actual strings, just use the provided
variables.
node serenity.js
When your code runs, you should get this output:
Start Workspace
Workspaces will shut down after 30 minutes of inactivity. Any running processes will
be stopped.
Start Workspace
querySelector
LessonDownloads
#header {
color: 'red';
.header {
color: 'red';
header {
color: 'red';
Each one of these sets the color to red. The only difference is in the selector; selecting
by ID, selecting by class, and selecting by tag. Got it? Good!
You've already learned the DOM methods to select by ID, class, and tag, too:
document.getElementById()
document.getElementsByClassName()
document.getElementsByTagName()
Three different methods that do almost the exact same thing. Wouldn't it be awesome if
there were a way to do element selecting similar to how CSS does it?
document.querySelector('#header');
// find and return the first element with the class "header"
document.querySelector('.header');
document.querySelector('header');
Demo Recap
In this demo:
document.querySelector('h2');
document.querySelector('.highlight-spanned');
Note: As we discussed earlier, the MDN UI has been updated and no longer uses
the highlight-spanned class. If you want to try this on your own (and I would
encourage you to do so!), try using the document-toc-heading class instead:
document.querySelector('h2');
document.querySelector('.document-toc-heading');
querySelectorAll
LessonDownloads
document.querySelectorAll('.header');
document.querySelectorAll('header');
Demo Recap
In this demo:
document.querySelectorAll('h2');
console.log('i is ' + i );
console.log(listOfElements[i]);
Recap
In this section, we took a brief look the history of browser support for standard DOM
methods, the rise of the jQuery library, and how jQuery's success brought about new
DOM methods. The new DOM methods we looked at are
document.querySelector('#header');
document.querySelectorAll('.header');
We also took a brief look that the list returned by .querySelectorAll() is a NodeList.
We saw that it is possible to loop over a NodeList with either its .forEach() method, or
the humble for loop:
Further Research
.querySelector() method on MDN(opens in a new tab)
.querySelectorAll() method on MDN(opens in a new tab)
NodeList on MDN
.getElementById()
.getElementsByClassName()
.getElementsByTagName()
.querySelector()
.querySelectorAll()
Along the way we talked about the web interfaces that facilitate this access,
including:
Element
Node
Up next, we'll learn new DOM methods that allow us to alter and control page
content!
Use Javascript and DOM methods to create, delete, and manipulate DOM
elements.
Understand the real power of combining JavaScript and the DOM.
document.getElementById()
document.querySelector()
Also, remember the Node Interface and the Element interface we went over in
the previous section? Understanding how an interface determines the
properties and methods for an element and understanding how to research an
interface's properties and methods will be vital in this lesson.
If you get stuck, just jump back to the previous section that's giving you a bit of
trouble.
Website Replica
Throughout this lesson, you'll learn skills and techniques to access and modify page
content. There's no better way to demonstrate these skills than through trying them out
on a real website! So I've created a replica of an older version of the Udacity homepage.
In a terminal, navigate to the folder where you want to store the code. Then use git
clone :
Alternatively, you can fork the repository to create an identical copy of the repository in
your own GitHub account and then clone the forked repository:
To load the website, drag the index.html file into your browser.
In your code editor, look for the app.js file. That is where you'll be doing all of your work
in this lesson.
The process is very similar to coding locally, but there are a few minor differences.
More Files!
In your workspace you'll see some files and a folder that are not included the course
GitHub repository.
package.json
package-lock.json
node_modules
Don't worry -- you don't need to touch these files. They are used to setup and run the
Node server that hosts the web page in a separate browser tab.
To see the web page, you'll need to click the OPEN THE WEB PAGE button at the
bottom of the screen.
That will open a new browser tab that contains the replica web page. The page will
update as you make changes.
Starting the server and opening the web page
innerHTML
textContent
innerText
By the end of this section, you'll know how to alter the HTML and text content of any
element on any website!
Let's jump right into it.
If you are using the workspace, you'll need to run npm run start in the terminal and
click on the VIEW THE WEB PAGE button in the lower right corner of the workspace.
Remember, if you are using the workspace it is recommended that you keep the
workspace open in a separate tab of your browser because we'll be going back to it
regularly throughout this lessson.
There's only one HTML file -- index.html -- so this is what it should look like after you
open it:
There's only one HTML file -- index.html -- so this is what it should look like after you
open it:
The index page of the Udacity website loaded in a browser.
index.html
The `index.html` serves as a starter template for you to get started. There
might be a slight mismatch between the actual content of `index.html` file
downloaded from GitHub vs. the one shown in the demo. However, the
mismatch will not block you from practicing the classroom instructions.
Write the DOM code necessary to select the first element with class: card
Submit
Let's store the first .card element in a variable for easy access:
Now that we've stored the first card element in the nanodegreeCard variable, we can
use nanodegreeCard to refer to this element instead of having to
use document.querySelector('.card') to select the element every time we need access to
it.
innerHTML
LessonDownloads
An Element's Inner HTML
Every element inherits properties and methods from the Element Interface (remember
this from the previous lesson!). This means that every element has
an .innerHTML property. This property, as it's rightly named, represents the markup of
the element's content. We can use this property to:
Demo Recap
In this demo, we:
1. Opened the replica home page either in our local coding environment or in the
Workspace.
2. Opened the console.
3. Selected the first element with the card class and saved it in a variable
named card :
card.innerHTML;
We saw card.innerHTML contains all of the HTML content of the selected element!
Question 1 of 2
What is .innerHTML ?
a property
a method
an element
an interface
Submit
Question 2 of 2
What does .innerHTML return?
a DOM element
a Node
a string
an array
an object
Submit
💡 Innie vs Outie 💡
The .innerHTML property sets or returns the HTML content inside the selected
element (i.e. between the tags).
There's also the rarely used .outerHTML property. .outerHTML represents the
HTML element itself, as well as its children.
Demo Recap
In this demo, we:
1. Selected the first element with the card class and saved it in a variable
named card :
card.innerHTML
We discovered that the innerHTML property contains all of the HTML for the element
while textContent only contains the element's text.
Check out the .textContent 's documentation page on MDN: textContent
docs(opens in a new tab)
Demo Recap
In this demo, we:
1. Selected the first element with the card class and saved it in a variable
named card :
card.textContent;
Quiz Question
<h1 id="test">Ice Cream Flavors</h1>
Given the HTML above, what will be the .textContent value after running this
code:
We just saw that passing text that contains HTML characters to .textContent will not
display the content as HTML. Instead, it will still display everything as text - even the
HTML characters!
If you'd like to update an element, including its HTML, then you need to use
the .innerHTML property:
// works as expected
myElement.innerHTML = 'The <strong>Greatest</strong> Ice
Cream Flavors';
Show TranscriptSummarize Video
Demo Recap
In this demo, we:
font-size: 1.5em;
heading;
5. Accessed the textContent of the saved element:
6. Oops! The <em></em> tags are rendered as text, not HTML. We fixed that
by using the innerHTML property instead:
Like the .textContent property, the .innerText property can be used to get/set an
element's text content, but there are some important differences between the two
properties.
.innerText , on the other hand, is a little trickier. Let's see this in action and then we'll
discuss it!
Show TranscriptSummarize Video
Demo Recap
In this demo, we:
1. Selected the first element with the card class and saved it in a variable
named card :
const card = document.querySelector('.card');
2. Selected the "Enroll Now" text on the card and hid it using CSS in
the Styles pane:
element.style {
display: none;
3. Accessed the textContent of the saved element -- it shows all of the text in
the element!:
card.textContent;
card.innerText;
When we access the text content with innerText , the hidden text ("Enroll Now") does
not appear and the "New" text appears in all uppercase letters. .innerText returns
the visible text of the element.
This is an important distinction! If CSS is used to hide any text inside that
element, .innerText will not return that text, while .textContent will return it. And it's
not just the hiding/showing nature of CSS that .innerText adheres
to, .innerText will also honor changes to things like capitalization.
The .textContent property has been a standard for quite a long time.
Conversely, .innerText property is a relatively new addition to the HTML specification.
It has been around for a while but was not fully supported by all browsers because it
was not a part of the HTML specification.
I like the name "Everything You Need to Know About Data" -- but you might have a
better idea.
When you are done, the page should look something like this:
Updated home page with updated title
Solution: Update Program Title
LessonDownloads
My Solution
Here's how I tackled this exercise:
.innerHTML
.textContent
.innerText
We saw that to set HTML content for an element, out of the three properties list above,
we can only use .innerHTML . Using .textContent will erroneously include the
HTML characters as plain text inside the element.
.createElement()
.creatextNode()
.appendChild()
.insertAdjacentHTML()
Are you ready to create some new content? Let's get started!
Question 1 of 2
You've learned about the Document object, the Node Interface, and the Element
interface. Where does .createElement() come from?
Submit
Now that you have found the documentation, take a minute to read through it to help
you answer the question below.
Question 2 of 2
Which of the following would create a new paragraph element?
document.createElement(p);
element.createElement('p');
document.createElement('p');
document.createElement('paragraph');
Submit
document.createElement('span');
document.createElement('h3');
Demo Recap
In this demo, we:
container
Next we'll learn how to add the new element to the page!
We can use the .appendChild() method to add an element to the page! Before we
see how this element works, let's quickly define the word "append". There are several
different definitions of the word, but I like the wording of the Cambridge Dictionary's the
best:
mainHeading.appendChild(newSpan);
I like the Cambridge Dictionary's version because it clearly states how the content is
added at the end. The .appendChild() method is called on one element, and is
passed the element to append. The element that is about to be appended is added as
the last child. So, in the example above, the <span> element will appear in the DOM
as a child of the <h1> ...but it will appear at the end, after all text and any other
elements that might be in the <h1> .
// causes an error
document.appendChild(newSpan);
Show TranscriptSummarize Video
Demo Recap
In this demo, we:
mainheading.appendChild(newSpanElement);
Just like you created new elements with the .createElement() method, you can also
create new text nodes using the .createTextNode() method. Take a look at the following
code that:
document.body.appendChild(myPara);
However, since you already know about the .textContent property, the code below will
provide the exact same result:
document.body.appendChild(myPara);
Therefore, instead of creating a new text node and appending it to an element, it's faster
and easier to just update the element's text with the .textContent property.
For more info, check out the documentation: createTextNode() docs(opens in a new
tab)
Quiz Question
What happens after running this code?
excitedText.textContent = '!!!';
mainHeading.appendChild(excitedText);
otherHeading.appendChild(excitedText);
The first argument to this method will let us insert the new HTML in one of four different
locations
At 0:22 seconds, the instructor says "... this has to be text, not HTML. If you pass
HTML, then that HTML will actually be displayed". There is a correction:
<p>
<!-- afterbegin -->
</p>
mainHeading.insertAdjacentHTML('afterend',
htmlTextToAdd);
1. Find the Community section. Hint: the class name for that section
is testimonials .
2. Create a new h2 element with the text Advice for New Udacity Students and
use appendChild to insert your new element at the end of the Community
section.
3. Use insertAdjacentHTML to add a p element with one or two sentences of
advice after the h2 element you created.
When you are done, the page should look something like this:
My Solution
Here's how I tackled this exercise:
const community =
document.querySelector('.testimonials');
// create element
community.appendChild(adviceHeader);
// create element
adviceHeader.appendChild(headerText);
community.appendChild(adviceHeader);
set reminders.</p>';
adviceHeader.insertAdjacentHTML('afterend',
adviceString);
Alternatively, I could have used insertAdjacentHTML with beforeend on
the community element:
.removeChild()
.remove()
.firstElementChild
.parentElement
a parent element
the child element that will be removed
<parent-element>.removeChild(<child-to-remove>);
Demo Recap
In this demo:
1. We found the hero_module div in the Elements pane and used $0 to store
the value of that element in a variable called cardContainer :
cardContainer.firstChild;
cardContainer.firstElementChild;
cardContainer.removeChild(firstCard);
And now the first card is gone from the page and the DOM!!!
However, we don't actually need to have the parent element because there is a
workaround! Just like the .firstElementChild property can be called on a parent
element to access its first element, every element also has a parentElement property
that refers to its parent! So if we have access to the child element that we're about to
add or remove, you can use the parentElement property to "move focus" to the
element's parent. Then we can call .removeChild() (or .appendChild() ) on that
referenced parent element.
mainHeading.parentElement.removeChild(mainHeading);
The preceding code will select the first <h1> on the page and stores it in
the mainHeading variable. Now to the next line:
mainHeading.parentElement.removeChild(mainHeading);
This starts with the mainHeading variable. It calls .parentElement , so the focus of
the next code is directed at the parent element. Then .removeChild() is called on the
parent element. Finally, mainHeading itself is passed as the element that needs to
be removed from its parent.
So an element uses itself to remove itself from its parent. Pretty cool, huh?
Removing a Child Element (Part 2!)
We went through all of those steps selecting an element, using DOM traversal
techniques like .parentElement and .firstElementChild , so that we can remove a
child element. I showed you this way so that you can get some exposure and practice to
moving around in the DOM.
Now, you might be glad (or frustrated! haha) to find out there's an easier way to do all
this! We can remove the child element directly with the .remove() method:
mainHeading.remove();
.removeChild()
.remove()
The difference is that with .removeChild() must be called on the parent of the
element being removed and must be passed the child to be removed,
while .remove() can be called directly on the element to delete.
.firstChild
.firstElementChild
.parentElement
The difference between .firstChild and .firstElementChild , is
that .firstElementChild will always return the first element,
while .firstChild might return whitespace (if there is any) to preserve the formatting of
the underlying HTML source code.
Further Research
removeChild on MDN(opens in a new tab)
remove on MDN(opens in a new tab)
firstChild on MDN(opens in a new tab)
firstElementChild on MDN(opens in a new tab)
parentElement on MDN
In this section, we'll be looking at controlling page and element styling using the following
properties and methods:
.style.<prop>
.cssText
.setAttribute()
.className
.classList
Before we get started, let's take a quick review of CSS specificity. This will come in handy as we
work on styling page content with JavaScript.
Quiz Question
Before we begin, put these in the correct order of CSS specificity. Put the least-
specific option at the top and the most-specific option at the bottom.
rules in a stylesheet
rules in a tag's style attribute
rules in a <style> tag
CSS Specificity
To be successful in this section, it will help to have an understanding of how
CSS Specificity works. According to the MDN, "specificity" is:
the means by which browsers decide which CSS property values are the most relevant
to an element and, therefore, will be applied.
Basically, the closer the style rule is to an element, the more specific it is. For
example, a rule in a style attribute on an element will override a style rule for
that element in a CSS stylesheet. There is also the specificity of the type of
selector being used. An ID is more specific than a class.
If you'd like to learn more about CSS Specificity, check out the MDN
documentation: CSS Specificity
mainHeading.style.color = 'red';
Now, I want to point out that when using the .style property, you can only
modify one CSS style at a time. That's why the previous code has .style.color =
'red' and not just .style = 'red' .
1. Found "Our Amazing Community" header in the DOM and used $0 to store
the value of that element in a variable called heading :
heading.style.color = 'orange';
heading.style.fontSize = '2em';
heading.style;
1. We scrolled through the style declaration to review the style attributes of the
element. We saw the two styles we added and then looked at
the cssText property, which consists of the two items we added: color:
orange; font-size:2em';
mainHeading.style.backgroundColor = 'orange';
mainHeading.style.fontSize = '3.5em';
...and that's just for setting three styles. Imagine if we needed 15 or 20 different styles!
So the .style.property syntax is perfect for setting one style at a time, but it's not
great for controlling multiple styles.
Fortunately, we can use the .style.cssText property to set multiple CSS styles at
once!
Notice that when using the .style.cssText property, you write the CSS styles just as
you would in a stylesheet; so you write font-size rather than fontSize . This is
different than using the individual .style.<property> way.
Quiz Question
<p id="quizzing-quizzes" style="color: orange;">Howdy</p>
Which of the following styles will change in the `#quizzing-quizzes" element after
running this code?
mainHeading.nextElementSibling.setAttribute('id',
'heading-sibling');
document.querySelector('#heading-
sibling').style.backgroundColor = 'red';
The last two lines could've been combined into one to bypass setting an ID and just
styling the element directly:
mainHeading.nextElementSibling.style.backgroundColor =
'red';
...but this was just to demonstrate that it's possible to set an attribute with JavaScript
that affects the DOM which then can be used immediately
Quiz Question
If you have this HTML element:
Which of these set the background color the description brown and the text to
white?
Hint: look at the code carefully. In some of the options there are tiny syntax
errors that will prevent the code from running the way you might expect.
Did you notice that I used background in the quiz above? That's
because background is a shorthand property that sets all of the background styles in
one statement, including the background color. You can read about it in the MDN
documentation: CSS - background(opens in a new tab) .
We violate that when we set styles using JavaScript. But to make our website
interactive, we often need to change the style of an element.
The .className property returns a string of all of the element's classes. If an element
has the following HTML:
console.log(listOfClasses);
mainHeading.className = 'im-the-new-class';
The above code erases any classes that were originally in the
element's class attribute and replaces it with the single class im-the-new-class .
console.log(arrayOfClasses);
arrayOfClasses.splice(i, 1);
arrayOfClasses.push('im-the-new-class');
mainHeading.className = newClassList;
That works -- but it is a lot of work! On the next page we'll explore an easier way to add
and remove classes from an element.
console.log(listOfClasses);
Demo Recap
In this demo, we:
1. Found "Our Amazing Community" header in the DOM and used $0 to store
the value of that element in a variable called heading:
heading.classList.add('ice-cream');
1. Removed the ice-cream class from the heading element using remove:
heading.classList.remove('ice-cream');
heading.classList.toggle('text-center');
heading.classList.toggle('text-center');
Question 3 of 3
What happens if you try to toggle a nonexistent class? For example, if you had
this HTML:
1. Add the new-hero class to the section with the hero--homepage class.
2. Add the new-card class to all of the elements with the card class.
Hint to make this change to all of the elements with the card class you'll need to use a
loop.
When you are done, the page should look like this:
Then I used the button's style property to set the background color and border radius.
start.style.backgroundColor = '#2015ff';
start.style.borderRadius = '5rem';
hero.classList.add('new-hero');
Then I looped through the list and added the new-card class to each element:
What do you think? Does the home page look better this way?
My recommendation to you is that, out of the list of techniques you learned in this
section, to use the .classList property more than any other. .classList is by far the
most helpful property of the bunch, and it helps to keep your CSS styling out of your
JavaScript code.
Further Research
style on MDN(opens in a new tab)
Article: Using dynamic styling information(opens in a new tab)
DOM methods to control styling(opens in a new tab)
nextElementSibling on MDN(opens in a new tab)
className on MDN(opens in a new tab)
classList on MDN(opens in a new tab)
Specificity on MDN
In this lesson, you'll learn how to run this DOM manipulating JavaScript code
in response to actions your users take.
Lesson Overview
To recap, we'll be looking at:
Website Replica
Throughout this lesson, you'll learn skills and techniques to access and modify page
content. There's no better way to demonstrate these skills than through trying them out
on a real website! So I've created a replica of an older version of the Udacity homepage.
Replica of an older version of the Udacity Home page
There are two ways for you to access the replica website:
Either way you'll get the same coding experience so choose whichever is easiest for
you to work with.
In a terminal, navigate to the folder where you want to store the code. Then use git
clone :
Alternatively, you can fork the repository to create an identical copy of the repository in
your own GitHub account and then clone the forked repository:
To load the website, drag the index.html file into your browser.
In your code editor, look for the app.js file. That is where you'll be doing all of your work
in this lesson.
The process is very similar to coding locally, but there are a few minor differences.
More Files!
In your workspace you'll see some files and a folder that are not included the course
GitHub repository.
package.json
package-lock.json
node_modules
Don't worry -- you don't need to touch these files. They are used to setup and run the
Node server that hosts the web page in a separate browser tab.
To see the web page, you'll need to click the OPEN THE WEB PAGE button at the
bottom of the screen.
That will open a new browser tab that contains the replica web page. The page will
update as you make changes.
Seeing An Event
There is a hidden world of events going on right now on this very page! It's really hard to
actually see into this hidden world, though. So how can we know that events
really are being announced? If they are being announced, how come they're not easy
for us to see?
Fortunately, the Chrome browser has a special monitorEvents() function that will let us
see different events as they are occurring.
Check out the documentation for the monitorEvents() function(opens in a new
tab) in the Console Utilities API reference page of the Chrome DevTools
documentation.
Demo Recap
We ran the monitorEvents to monitor the document object:
monitorEvents(document);
click
dblclick
scroll
The monitorEvents function will keep spitting out all of the events that are
happening on the targeted element until the end of time...that, or until you
refresh the page. Alternatively, the Chrome browser does offer
an unmonitorEvents() function that will turn off the announcing of events for
the targeted element:
unmonitorEvents(document);
Event Targets
LessonDownloads
The EventTarget Interface
Do you remember the Node Interface and the Element interface from the first lesson?
Do you remember how the Element Interface is a descendant of the Node Interface,
and therefore inherits all of Node's properties and methods?
Well there was one piece that I totally skipped over then but am addressing now. The
Node Interface inherits from the EventTarget Interface.
is an interface implemented by objects that can receive events and may have listeners
for them.
and
Element, document, and window are the most common event targets, but other objects
can be event targets too…
As you can see from the image above, the EventTarget is at the top of the chain. This
means that it does not inherit any properties or methods from any other interfaces.
However, every other interface inherits from it and therefore contain its properties and
methods. This means that each of the following is an "event target";
If you take a look at the EventTarget Interface, you'll notice that it doesn't
have any properties and only three methods! These methods are:
.addEventListener()
.removeEventListener()
.dispatchEvent()
The one that we'll be looking at for the rest of this lesson will be
the .addEventListener() method.
...all of these mean the same thing and are interchangeable with one another.
<event-target>.addEventListener(<event-to-listen-for>, <function-to-
run-when-an-event-happens>);
So an event listener needs three things:
The <event-target> (i.e. the target) goes right back to what we just looked at:
everything on the web is an event target (e.g. the document object, a <p> element,
etc.).
The <event-to-listen-for> (i.e. the type) is the event we want to respond to. It could
be a click, a double click, the pressing of a key on the keyboard, the scrolling of the
mouse wheel, the submitting of a form...the list goes on!
mainHeading.addEventListener('click', function () {
});
Check out the MDN documentation for more info: addEventListener docs(opens in a
new tab).
Let's Add an Event Listener!
Show TranscriptSummarize Video
Demo Recap
In this demo, we:
})
3. Clicked on the page 22 times, which logged the "The page was clicked"
message to the console 22 times!
Demo Recap
In this demo, we:
1. Opened the index.html file in our code editor to see where our app.js file is loaded.
2. Opened the app.js file and added an event listener:
document.addEventListener('click', function() {
})
3. Loaded the HTML page in the browser (or reloaded it if it was already running) and
opened the console.
4. Clicked on the page to see the message in the console
5. Edited the event listener to target the <h1> element
document.addEventListener('click', function() {
mainHeading.style.backgroundColor = 'red';
})
7. Refreshed the page to reload the app.js file and clicked again. The element's
background color turned to red!
8. Checked the DOM to confirm that the style background-color: red had been added to
the element.
This works just like it does when we run code in the console -- except now we
are running it in our JavaScript file so it will run whenever the page is loaded
or refreshed!
So far, we've only looked at the "click" event and a couple of other ones. When we
used the monitorEvents() function in the previous section, we saw a number of different
event types (e.g. dblclick , scroll , resize ).
How do you know what events are even out there to listen for? The answer is easy -
documentation! To see a full list of all of the possible events you can listen for, check
out the Events documentation: list of events(opens in a new tab)
Hint: you'll need more than one event type to make this work.
Task List
Write all of your code in the app.js file of the replica home page project.
Change the text of the h1 element to "Build an AR App" when a user's mouse is over
the h1 element.
Change the text of the h1 element back to "Learn ARKit" when a user's mouse moves
off of the h1 element.
Move your mouse over the h1 element to confirm that your code works
4. Add event listener to respond to moving the mouse over the selected element
('mouseover' event):
header.addEventListener("mouseover", function () {
header.textContent = updatedText;
});
5. Add event listener to respond to moving the mouse off of the selected element
('mouseout' event):
Recap
In this section, you learned all about events, the EventTarget Interface, and how to add
event listeners. We used the .addEventListener() method to attach listeners to:
the document
a Node
an Element
...basically anything that inherits from the EventTarget Interface. We also saw that there
are three main parts to an event listener:
When working with JavaScript objects and equality, you need to determine if
they are they two separate objects or are they different names referring to the
same object.
Hopefully, this is all review. But let's talk about just object equality, which includes
objects, arrays, and functions. Try giving these quizzes a shot:
Question 2 of 3
Given this code:
const a = {
const b = {
};
true
false
Submit
Question 3 of 3
Given this code:
const a = {
myFunction: quiz
};
const b = {
myFunction: quiz
}
Does the following code evaluate to true or false?
true
false
Submit
So why do we care about any of this object/function equality? The reason is that
the .removeEventListener() method requires you to pass the same exact listener
function to it as the one you passed to .addEventListener().
<event-target>.removeEventListener(<event-to-listen-for>,
<function-to-remove>);
Remember, the listener function must be the exact same function as the one used in
the .addEventListener() call...not just an identical looking function. Let's look at a couple
of examples.
This code will successfully add and then remove an event listener:
function myEventListeningFunction() {
console.log('howdy');
document.addEventListener('click',
myEventListeningFunction);
document.removeEventListener('click',
myEventListeningFunction);
Now let's look at an example that would not work (it does not remove the event
listener):
document.addEventListener('click', function
myEventListeningFunction() {
console.log('howdy');
});
// immediately removes the click listener that should run
the `myEventListeningFunction` function
document.removeEventListener('click', function
myEventListeningFunction() {
console.log('howdy');
});
This code does not successfully remove the event listener. Again, why does
this not work?
Two functions can look the same, but live in two different places in memory. Looks can
be deceiving!
When we wrote
function myEventListeningFunction() {
console.log('howdy');
}
a second time, we actually created a completely new function that was stored in a
completely new location in memory! They may look the same and do the same thing,
but that doesn't make them the same. Imagine a scenario where you see two houses
that look exactly the same. While the houses might look the same, their addresses are
different! That's exactly what just happened in our previous example.
Quiz Question
Assuming that myForm is a <form> element, will the <form> element have
a submit event listener after running the following code, or not?
myForm.addEventListener('submit', function
respondToSubmit(){...});
myForm.removeEventListener('submit', function
respondToSubmit(){...});
Demo Recap
In this demo, we:
1. Opened the app.js file and added an event listener which logs a message and
remove the first child element of the element with the #contain-all class:
document.addEventListener('keypress', function() {
document.querySelector('#contain-
all').firstElementChild.remove();
})
2. Found the #contain-all element, selected it and reviewed the Event Listeners panel.
No event listeners on that element!
3. Clicked on the Ancestors option. Now we can see our keypress event listener on
the body element.
4. Pressed a key on the page several times and watched elements disappear from the
page.
We're going to create an event listener that removes the last of the featured programs in
the section of the page with the hero_module class, and then removes itself so it
can't be used again.
This is also a good opportunity to get familiar with the Event Listeners tab in Dev
Tools. After you have written your event listener, go into the Event Listeners tab and
explore the listener you created:
My Solution
1. I selected the hero element:
2. I created function that runs a event listener to remove the last child element in
the hero module:
function removeElementOnce() {
hero.lastElementChild.remove();
document.addEventListener('click', removeElementOnce);
4. I tested my code. It works!!! But I'm not done yet because the listener is still
active after the first click.
Testing my event listener
It works -- but it isn't disabled after the first click
function removeElementOnce() {
hero.lastElementChild.remove();
document.removeEventListener('click',
removeElementOnce);
}
Recap
In this section, you learned about how to remove event listeners. You took a dive into
object equality and how that plays a huge part in removing an event. Lastly, we also
looked at how you can find out what event listener a DOM element has by using the
DevTools.
What's Next?
Now that we've learned about adding and removing event listeners, it's time to learn
about the phases of an event!
Further Research
removeEventListener on MDN(opens in a new tab)
Easily jump to event listeners(opens in a new tab)
Equality comparisons and sameness(opens in a new tab)
Article: Object Equality in JavaScript(opens in a new tab)
EventTarget Interface
Phases of an Event
LessonDownloads
Free Response
Thinking about nodes and how the DOM is structured, after running the code below,
which event listener will fire first when the page is clicked?
DO NOT RUN THE CODE YET. Just jot down your prediction and why you think your
prediction is correct.
document.addEventListener('click', function () {
console.log('The document was clicked');
});
document.body.addEventListener('click', function () {
console.log('The document body was clicked');
});
Submit
Event Phases
There are three different phases during the lifecycle of an event. They are:
And they actually follow the order above; first, it's capturing, then at target, and then
the bubbling phase.
Most event handlers run during the at target phase, such as when you attach a click
event handler to the button. The event arrives at the button (its target), and there's only
a handler for it right there, so the event handler gets run.
But sometimes you have a collection of items -- such as a list -- and want to have one
handler cover every item (and still have the option of individual handlers for some
items.) By default, if you click on a child item and a handler doesn't intercept the click,
the event will "bubble" upward to the parent, and keep bubbling until something handles
it or it hits the document.
Capturing, on the other hand, lets the parent intercept an event before it reaches a
child.
Let's dig into these phases to see how they affect when events fire and the order they
fire in!
Example Code:
To see the phases of an event, let's use this HTML code as an example.
<html>
<body>
<p>
</p>
</div>
</body>
</html>
1. Clicking on the event starts the capturing phase at the html tag, working its
way down to the element that was clicked.
2. When it reached the element that was clicked, it's in the target phase.
3. Then it switches to the bubbling phase and works its way back to the html tag.
The three event phases of an event: capturing, at target, and bubbling.
So of the three phases in an event, which one does the .addEventListener() method
actually use? And, how can we change it?
Up until this point, we've only seen the .addEventListener() method called
with two arguments, the:
event type
and the listener
document.addEventListener('click', function () {
console.log('The document was clicked');
});
The code below uses .addEventListener() with only two arguments, so it will invoke
the listener during the bubbling phase:
document.addEventListener('click', function () {
});
However, in this code, .addEventListener() is called with three arguments with the
third argument being true (meaning it should invoke the listener earlier, during the
capturing phase!).
document.addEventListener('click', function () {
console.log('The document was clicked');
}, true);
Testing my event listener again
Now it works only on the first click
6. I refreshed the page and explored the Event Listeners tab in Dev Tools to find
the function that is triggered when the page is clicked and then manually
removed the event listener.
HINT: Remember the event listener is registered to document so you'll need to toggle
on the Ancestors button on so you can see the event listener.
View the function and remove the listener in the Event Listeners tab
HTML
<html>
<body>
<p>
</p>
</div>
</body>
</html>
Capturing
The true parameter at the end of the .addEventListener method makes the
event respond in the capturing phase:
// runs first in capturing phase
document.querySelector('p').addEventListener('click', function () {
}, true);
Bubbling
These listeners will fire in the bubbling phase because that is the default
for .addEventListener :
document.querySelector('body').addEventListener('click', function () {
});
// runs second after target phase is completed and bubbling phase starts
document.querySelector('button').addEventListener('click', function () {
});
The event finishes when it reaches the html element at the top of the chain.
Quizzes: Event Phases
LessonDownloads
Question 1 of 2
Look at the code below. In which phase will the event fire?
el.addEventListener('click', function () {
console.log('You clicked on the 2nd quizzing-quizzes item!');
}, false);
Now that you have a little more knowledge about the "capturing", "at target", and
"bubbling" phases, we're going to go back to the question at the beginning of this
section.
Question 2 of 2
After running the code below and clicking on the page, two console.log
messages will display in the console. Put the messages in the correct order.
document.addEventListener('click', function () {
console.log('The document was clicked');
});
document.body.addEventListener('click', function () {
console.log('The body element was clicked');
});
The body element was clicked.
The document was clicked.
Order of Messages
console.log Message
First Message
Second Message
Submit
When an event occurs, the browser includes an event object. This is just a regular
JavaScript object that includes a ton of information about the event itself. According to
MDN, the .addEventListener() 's listener function receives:
a notification (an object that implements the Event interface) when an event of the
specified type occurs
Up until this point, I've been writing all of the listener functions without any parameter to
store this event object. Let's add a parameter so we can store this important
information:
});
Notice the new event parameter that's been added to the listener function. Now when
the listener function is called, it is able to store the event data that's passed to it!
const el = items[1];
});
evt
e
theEvent
horse
The name event or evt does not provide any inherent meaning or special capabilities;
there is nothing special to the name... it's just the name of the parameter. Feel free to
give it any name that's informative or descriptive!
Demo Recap
In this demo, we:
console.log(event);
});
2. Opened the logged event in the console and explored it. We looked at:
pageX and pageY which give us the coordinates of the click event on the
page
type which gave us the type of the event
Think about an anchor link on a webpage. There are probably a couple dozen links on
this page! What if you wanted to run some code and display some output when you
click on one of these links. If you click on the link, it will automatically navigate you to
the location listed in its href attribute: that's what it does by default.
What about a form element? When you submit a form, by default, it will send the data to
the location in its action attribute. What if we wanted to validate the data before
sending it, though?
Without the event object, we're stuck with the default actions. However, the event object
has a .preventDefault() method on it that a handler can call to prevent the default
action from occurring!
});
Try This!
Load the replica homepage project in the browser and click the My Classroom link in
the uppper-right corner. The browser will navigate to your Udacity classroom.
event.preventDefault();
});
What happens now? You should see the message in the console and you should stay
on the replica home page.
Quiz Question
Any event that is cancelable will be stopped with .preventDefault() . Which of
these are cancelable?
Hint: If you aren't sure, check out the W3C documentation: List of Event
Types(opens in a new tab)
click
mouseenter
mouseover
load
keyup
Submit
Events
LessonDownloads
Recap
We covered a number of important aspects of events and event listeners in this section!
We looked at:
the phases of an event:
the capturing phase
the at target phase
the bubbling phase
the event object
prevent the default action with .preventDefault()
Further Research
Event dispatch and DOM event flow(opens in a new tab) on W3C
capture phase(opens in a new tab) on W3C
target phase(opens in a new tab) on W3C
bubble phase(opens in a new tab) on W3C
Event(opens in a new tab) on MDN
Event reference(opens in a new tab) on MDN
addEventListener(opens in a new tab) on MDN
});
myCustomDiv.appendChild(newElement);
document.body.appendChild(myCustomDiv);
Submit
Demo Recap
In this demo we
1. Added the code from the quiz above to the app.js file in our replica web
page:
newElement.addEventListener('click', function
respondToTheClick(evt) {
});
myCustomDiv.appendChild(newElement);
document.body.appendChild(myCustomDiv);
2. Refreshed the page, which added 200 new elements to the bottom of the page
3. Inspected the event listener on the paragraph elements.
All of the paragraphs have the an identical click event listener -- and each listener
creates a new, identical respondToTheClick function, like this:
There must be a better way...
newElement.addEventListener('click', function
respondToTheClick() {
});
myCustomDiv.appendChild(newElement);
document.body.appendChild(myCustomDiv);
We're creating a <div> element, attaching two hundred paragraph elements and
attaching an event listener with a respondToTheClick function to each paragraph as we
create it. There are a number of ways we could refactor this code. For example, as of
right now, we're creating two hundred different respondToTheClick functions (that all
actually do the exact same thing!). We could extract this function and just reference the
function instead of creating two hundred different functions:
function respondToTheClick() {
console.log('A paragraph was clicked.');
}
newElement.addEventListener('click',
respondToTheClick);
myCustomDiv.appendChild(newElement);
}
document.body.appendChild(myCustomDiv);
Better! One listener helps reduce complexity but we still have too many listeners
This is a great step in the right direction!
However, we still have two hundred event listeners. They're all pointing to the same
listener function, but there are still two hundred different event listeners.
function respondToTheClick() {
myCustomDiv.appendChild(newElement);
myCustomDiv.addEventListener('click', respondToTheClick);
document.body.appendChild(myCustomDiv);
Now the browser doesn't have to store in memory two hundred different event listeners
and two hundred different listener functions. That's great for performance`!
However, if you test the code above, you'll notice that we've lost access to the individual
paragraphs. There's no way for us to target a specific paragraph element. So how do
we combine this efficient code with the access to the individual paragraph items that we
did before?
Event Delegation
LessonDownloads
Event Delegation
Remember the event object that we looked at in the previous section? That's our ticket
to getting back the original functionality!
The event object has a .target property. This property references the target of the
event. Remember the capturing, at target, and bubbling phases?...these are coming
back into play here, too!
Let's say that you click on a paragraph element. Here's roughly the process that
happens:
So event.target gives us direct access to the paragraph element that was clicked.
Because we have access to the element directly, we can access its .textContent ,
modify its styles, update the classes it has - we can do anything we want to it!
function respondToTheClick(evt) {
console.log('A paragraph was clicked: ' +
evt.target.textContent);
}
myCustomDiv.appendChild(newElement);
}
document.body.appendChild(myCustomDiv);
myCustomDiv.addEventListener('click', respondToTheClick);
<article id="content">
</article>
In this filler text(opens in a new tab) , notice that there are some <span> tags. If we
want to listen to the <article> for a click on a <span> , you might think that this
would work:
document.querySelector('#content').addEventListener('clic
k', function (evt) {
console.log('A span was clicked with text ' +
evt.target.textContent);
});
This will work, but there's a major flaw. The listener function will still fire when either one
of the paragraph elements is clicked, too! In other words, this listener function is not
verifying that the target of the event is actually a <span> element. Let's add in this
check:
document.querySelector('#content').addEventListener('clic
k', function (evt) {
});
Remember that every element inherits properties from the Node Interface(opens in a
new tab). One of the properties of the Node Interface that is inherited is .nodeName .
We can use this property to verify that the target element is actually the element we're
looking for. When a <span> element is clicked, it will have a .nodeName property
of "SPAN" , so the check will pass and the message will be logged. However, if
a <p> element is clicked, it will have a .nodeName property of "P" , so the check
will fail and the message will not be logged.
Question 1 of 2
Review the HTML below:
<div class="container">
<ul>
</ul>
</div>
Which of these code snippets would remove the first list item?
document.querySelector('.container').addEventListener('click',
function (e) {
if (e.target.className == 'items') {
e.target.remove();
}
});
document.querySelector('.container').addEventListener('click',
function (e) {
if (e.target.id == 'item-1') {
e.target.remove();
}
});
document.querySelector('.container').addEventListener('click',
function (e) {
e.target.remove();
});
Submit
Question 2 of 2
Review the HTML below:
<div class="container">
<ul>
</ul>
</div>
Which of these code snippets would remove ONLY the first list item?
document.querySelector('.container').addEventListener('click',
function (e) {
if (e.target.id == 'item-1') {
e.target.remove();
}
});
document.querySelector('.container').addEventListener('click',
function (e) {
if (e.target.className == 'items') {
e.target.remove();
}
});
document.querySelector('.container').addEventListener('click',
function (e) {
e.target.remove();
});
document.querySelector('.container').addEventListener('click',
function (e) {
if (e.target.textContent.includes('Item 1')) {
e.target.remove();
}
});
Submit
In this exercise, we will add buttons to the Nanodegree cards in the section of the page with
the list-nanodegrees class.
There is a lot to do here! This section will test many of the skills you have used so far:
Selecting elements
Creating new elements and appending them to the page
Updating elements
Removing elements
Preventing default actions
Using event delegation and event targets
When you are done, the Nanodegree list section should look like this:
A Few Hints
Think about which element to target for your NodeList. You'll want to select an element where you can
use appendChild to add the button to the card after the title and before the affiliates.
Think about where to use preventDefault . You'll want the user to be able to navigate to the
Nanodegree page to learn more about it when they click any part of the card other than the button.
Style the buttons using existing classes. The button and button--primary classes will apply the
same styles as the rest of the buttons o the page.
Think outside the box when you are removing the favorite status indicator. There are many ways to do
this, but the simplest is to directly target the element you want to remove. You should be familiar with
the parent and child properties of the Node Interface(opens in a new tab) -- but did you know that
there are sibling properties too?
Task List
Write all of your code in app.js .
Select elements to create a NodeList that holds the nodes you'll use to append the favorite
buttons.
My Solution
ndList.forEach(function (nd) {
nd.appendChild(button);
});
function toggleFavorite(event) {
1. Once I've confirmed that the user clicked the button, I want to run the code to
prevent the default action of navigating away from the page:
event.preventDefault();
1. I checked to see if the item is already a favorite. If it isn't, I change the text and
create a new p element to hold the favorite status, and append the new
element to the page:
if (target.textContent.startsWith('Add')) {
target.parentElement.appendChild(fav);
1. If the item is already a favorite, I need to change the text back to the original
text and remove the favorite status indicator using the
target's nextSibling property:
} else {
target.nextSibling.remove();
function toggleFavorite(event) {
event.preventDefault();
if (target.textContent.startsWith('Add')) {
target.parentElement.appendChild(fav);
} else {
event.target.textContent = 'Add to
favorites';
target.nextSibling.remove();
}
}
Recap
In this section, we looked at Event Delegation. Event Delegation is the process of
delegating to a parent element the ability to manage events for child elements. We were
able to do this by making use of:
Further Research
Article: Event delegation(opens in a new tab)
Article: How JavaScript Event Delegation Works
In this section, we'll look at why this happens and how to avoid this problematic
situation.
Let's look at some code to show (more or less) what's happening. Take a look at this
initial part of an HTML file:
<!DOCTYPE html>
<html lang="en">
<head>
<script>
document.querySelector('footer').style.backgroundColor =
'purple';
</script>
This isn't the full HTML file...BUT, it's all that's been parsed so far. Notice at the bottom
of the code that we have so far is a <script> file. This is using inline JavaScript rather
than pointing to an external file. The inline file will execute faster because the browser
doesn't have to make another network request to fetch the JavaScript file. But the
outcome will be exactly the same for both this inline version and if the HTML had linked
to an external JavaScript file.
Do you see the JavaScript/DOM code in the <script> tags? Take a second and read
it again:
document.querySelector('footer').style.backgroundColor =
'purple';
Does anything jump out at you about this code? Anything at all? This code is completely
error-free...unfortunately, when it runs, it will still cause an error. Any ideas why?
null.style.backgroundColor = 'purple';
Quiz Question
Take a look at this code. When will an error be thrown?
HTML
<html>
<head>
<meta charset="utf-8">
<script src="app.js"></script>
</head>
<body>
<div class="container">
<p>
<button>Click Me!</button>
</p>
</div>
</body>
</html>
JavaScript
myButton.addEventListener('click', function () {
myButton.style.backgroundColor = 'red';
});
myButton.addEventListener('click', function () {
When the button is clicked and JavaScript tries to run this code:
myButton.style.backgroundColor = 'red';
The code will run as expected. When a user clicks on the button its background color
will turn red and no error will be thrown.
Submit
Think about why this would make things work. Well, if the DOM is built sequentially, if
the JavaScript code is moved to the very bottom of the page, then by the time the
JavaScript code is run, all DOM elements will already exist!
DOMContentLoaded
LessonDownloads
The Content Is Loaded Event
When the document object model has been fully loaded, the browser will fire an event.
This event is called the DOMContentLoaded event, and we can listen for it the same
way we listen to any other events:
document.addEventListener('DOMContentLoaded', function () {
console.log('the DOM is ready to be interacted with!');
});
Show TranscriptSummarize Video
Demo Recap
In this demo we:
Question 1 of 2
We want to add an event listener to trigger a notification for
the DOMContentLoaded event. Where should we register the listener?
On the document object
<!DOCTYPE html>
<html lang="en">
<head>
<script>
document.addEventListener('DOMContentLoaded', function () {
document.querySelector('footer').style.backgroundColor = 'purple';
});
</script>
Pretty cool, right?!? We have the JavaScript code in the <head> element, but it is
now wrapped in an event listener for the DOMContentLoaded event. This will
prevent the DOM-styling code from running when the browser gets to it. Then, when the
DOM has been constructed, the event will fire and this code will run.
If you're looking at somebody else's code, you may see that their code listens for
the load event being used instead (e.g. document.onload(...) ). load fires later
than DOMContentLoaded -- load waits until all of the images, stylesheets, etc.
have been loaded (everything referenced by the HTML.) Many older developers
use load in place of DOMContentLoaded as the latter wasn't supported by the very
earliest browsers. But if you need to detect when your code can
run, DOMContentLoaded is generally the better choice.
However, just because you can use the DOMContentLoaded event to write
JavaScript code in the <head> that doesn't mean you should do this. Doing it this
way, we have to write more code (all of the event listening stuff) and more code is
usually not always the best way to do something. Instead, it would be better to move the
code to the bottom of the HTML file just before the closing </body> tag.
So when would you want to use this technique? Well, JavaScript code in
the <head> will run before JavaScript code in the <body> , so if you do have
JavaScript code that needs to run as soon as possible, then you could put that code in
the <head> and wrap it in a DOMContentLoaded event listener. This way it will
run as early as possible, but not too early that the DOM isn't ready for it.
<html>
<head>
<meta charset="utf-8">
<script src="app.js"></script>
</head>
<body>
<div class="container">
<p>
<button>Click Me!</button>
</p>
</div>
</body>
</html>
JavaScript
myButton.addEventListener('click', function () {
myButton.style.backgroundColor = 'red';
});
Move the script tag to right before the closing </body> tag
Keep the script tag in the <head> element and add
a DOMContentLoaded listener to the top of the JavaScript file, like this:
document.addEventListener('DOMContentLoaded', function () {
console.log('the DOM is ready!');
});
myButton.addEventListener('click', function () {
myButton.style.backgroundColor = 'red';
});
Keep the script tag in the <head> element and wrap the JavaScript code in
a DOMContentLoaded listener, like this:
document.addEventListener('DOMContentLoaded', function () {
const myButton = document.querySelector('button');
myButton.addEventListener('click', function () {
myButton.style.backgroundColor = 'red';
});
});
Submit
Recap
In this section, we learned about the helpful DOMContentLoaded event.
Along the way, we reviewed how the HTML code is parsed incrementally and how this
affects JavaScript/DOM code. We also looked at why writing DOM-manipulation code in
the <head> can cause errors.
Further Research
DOMContentLoaded Event docs on MDN(opens in a new tab)
You now know how to use JavaScript to interact with the browser.
Congratulations on your new superpower!
Certification Assessment