A value is a location in computer memory that stores DATA.

There are many kinds of values, including String, Number, Array, Date, ...

Different kinds of values are called types.

We will be focusing on the most common types in this course


A number is what it sounds like -- any integer or decimal.



A string is an object that's a collection of characters, like a word or a sentence.

"Cherry Pie"


A boolean is a value that is either true or false.

(It's named after George Boole, a 19th-century mathematician who invented Boolean algebra.)

Comments - Single Line

When reading JavaScript code, if you ever see two slashes in a row, that means "everything after these slashes is a comment".

2 + 2    // makes four

A comment is a message for humans. JavaScript ignores everything to the right of the slashes, so you can explain what the nearby code does, or why it does it.

In these lessons, we often use comments to explain the result of executing the nearby code. In this case, we sometimes add an arrow to the comment:

2 + 2  //=> 4
3 + 5 // -> 8

Comments - Multi Line

JavaScript also has multi-line comments via /* I am a comment */

They can be used to comment out a section within a line, or many lines

let x = 42;

  This is
  a multiline

console.log(x /* the value from earlier */);
// => 42


Values can be combined or manipulated using operators, like...

All JavaScript Operators

An operator sends a message to the value

Dot is a special operator that sends arbitrary messages; we will learn more about her later.

LAB: Number Games

We can use numbers and operators together to do math

Use the node program to the answers to the following


let color = "blue"
let fruit = "berry"
color + fruit      // "blueberry"
'black' + fruit    // "blackberry"

color = 'pink'
color + fruit      // "pinkberry"

LAB: Strings with Whitespace

let fruit = 'pineapple'
let food = 'smoothie'

How could you use the above variables to get the following?

pineapple smoothie

^^^ Note that there is a space between the strings ^^^

The Warehouse Metaphor

Think of memory as a giant warehouse.

Warehouse from Raiders of the Lost Ark

Like this warehouse from the movie Raiders of the Lost Ark, computer memory is vast and filled with boxes of various sizes.

The Warehouse Metaphor Explained

If memory is a giant warehouse...

...and memory locations are boxes in that warehouse

...then a value is the contents of a box

...and a variable is a label you stick on the outside of the box

Let there be confusion

Don't let me down

Unfortunately, in JavaScript you can only use let once per variable name in a given scope, otherwise you will receive an error like this:

Identifier 'x' has already been declared

That means that when you're in the JavaScript console, if you see this error then try again without the let

> let x = 1
> let x = x + 2
SyntaxError: Identifier 'x' has already been declared
> x = x + 2

String Literals

"He said 'My dog has fleas.' yesterday"
'Vermonters have a hundred words for "snow".'

String Escapes

console.log("Roses are red,\nViolets are blue;\nCandy is sweet,\nAnd so are you.")

String Messages

A string understands lots of messages. Here are a few:

"drive" + "way"
'Java' + "Script"

"Bert's pal Ernie" + ' sings "Rubber Duckie"'






"blueberry".replace("blue", "black")

Try all of these out in node or the browser console!

Check out MDN String docs for more.

Slicing and Dicing

Every string is made of lots of other strings.

You can pull out parts of a string with the slice message.

// this means "slice from character 0 to character 4"
"blueberry".slice(0, 4)

// this means "slice from character 4 to the end

These start and end numbers are called indexes (or indices if you're feeling fancy).

MDN: slice

String Indexing Explained

Humans like to start counting at 1, but computers like to start counting at 0.

This can be confusing, so here's a visualization to help explain it.

Think of the indexes as pointing at the spaces between characters, as in this diagram:

| B | L | U | E | B | E | R | R | Y |
0   1   2   3   4   5   6   7   8   9

So with this picture in your mind, slice...

Try various start and end values in the console and see what happens!

LAB: Cut the Cream

How could you take the following string "banana cream pie" and assign a variable to "cream" ?

// starting string
let favoritePie = 'banana cream pie';

// variable to assign to "cream"
let withExtra = favoritePie.slice(/* What goes here? */);


MDN Docs for Number

Order of operations

Q: What is 1 plus 2 times 3?

A: It depends!

(1 + 2) * 3 == 9
1 + (2 * 3) == 7

Parentheses Are Free

When in doubt, use parentheses!

(or, when there's any doubt, make your assumptions explicit)

LAB: one nation, divisible

One number is divisible by another if you divide them and the remainder is 0.

Write code in node to figure out if...

Strings vs. Numbers


1 + 2
"1" + "2"
"1 + 2"

Strings plus Numbers

Hmmm again...

"1" + 2

Even though Strings and Numbers are different TYPES, JavaScript converts one to the other.

But when two types meet, which one wins?

Type Conversion

The clearest answer is that you, the programmer, explicitly declare which type you want to win.

Converting a Number to a String

(12).toString() // "12"

Converting a String to a Number

There are many ways to convert a string to a number in JavaScript.

The easiest and cleanest is unary +:

expression value
+"12" 12
+"012" 12
+"0.2" 0.2
+"cheese" NaN
+"0" 0
+"" 0
+" " 0

(Other ways can give bizarre results if the string contains letters or begins with a 0.)

Converting a String to a Numer - ParseInt

There are two other common methods to convert strings to numbers which are shown below

// Converts strings to integers
parseInt("42") // => 42

// Converts strings to floating point numbers
parseFloat("3.1418") // => 3.1418

Type Coercion

If you don't explicitly do type conversion then JavaScript will "helpfully" convert the types for you.

This is called type coercion and just like with people, coercion is stressful and often backfires.

For instance, if you ask the user their age, and read it from a web form or keyboard input, it will be in a string. If you forget to explicitly convert that to a number before using it in a calculation, the results can be unexpected...

  > "30" - 1              // "30" is coerced into a number
  > "30" + 1              // 1 is coerced into a string

(You Don't Know JS has way more information than you wanted to know on this subject.)

Randomize it!

Math.random is your way to make things unpredictable!

Calling Math.random() returns a random decimal number between 0 and 1.

If you want a random integer you can multiply that decimal by a range, like this:

function randomInteger(min, max) {
  let range = max - min + 1;
  return min + Math.floor(Math.random() * range);

console.log(randomInteger(1, 3));


What makes an array an array

Creating an array

["apple", "banana", "cherry"]

square brackets on their own mean "please go create an array now"

and put these 3 other values inside it

Array Indexes

let fruits = ["apple", "banana", "cherry"];

...but which fruit? See next slide!

LAB: Array Indexes Exercise

Try this in node:

let fruits = ["apple", "banana", "cherry"]

Did you get the result you expected?

Why or why not?

Start At Zero

When counting,

humans start at one,

but computers start at zero.

So the first item in an array is number zero, not number one.


All arrays have a special property named length

What do you think it does?

let fruits = ["apple", "banana", "cherry"]
fruits.length //=> 3

Question: How can you get the last item in an array... even if you don't know its index beforehand?

The End

Answer: Use the Array's length proprty within the indexing

let fruits = ["apple", "banana", "cherry"]
fruits[fruits.length - 1]

After The End

Try this:


Did you get the result you expected?

Why or why not?

Undefined means 🤷

by returning undefined, the computer is answering the question

"What is the 99th item?"

with the answer

"There is no 99th item."

 Previous Next