No site found for codelikethis.herokuapp.com; using CodeLikeThis content.


 Slides

Values

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

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

(The different kinds of values are called TYPES. Soon you will create your own types but for now, we will use the built-in ones.)

Numbers

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

10
-12
3.14

Strings

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

"apple"
"banana"
"Cherry Pie"

Booleans

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.)

Operators

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

  • PLUS (+)
  • TIMES (*)
  • POWER (**)
  • DOT (.)

An operator sends a message to the value

  • e.g. 1 + 2 sends the number 1 the message please add 2 to yourself.

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

Comments

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

JavaScript also has multi-line comments via /* ... */ but those are less common.

Expression Evaluation

A snippet of JavaScript code is called an expression.

Whenever JavaScript encounters an expression, it tries to evaluate it, which means to convert it into a value.

A simple expression (like a plain number or a string) evaluates to just that value.

A more complicated expression with operators keeps applying those operators until it gets down to a single value.

You can think of evaluation as asking and answering a question.

2 + 2    // Question: What is 2 + 2?
4        // Answer: 4

// Q: What is the all-caps version of the string "apple"?
"apple".toUpperCase()  
// A: the string "APPLE"
"APPLE"

We say that a statement evaluates to a value, as in "2 plus 2 evaluates to 4". You can also say "the value of 2 + 2 is 4" or "the return value of 2 + 2 is 4".

Return Values

Sometimes the return value is the same as the original value.

4 * 1    // return value: 4

Sometimes the return value is a different value.

2 + 3    // return value: 5

Sometimes the return value is a different value and a different type.

"banana".length  // return value: 6

Sometimes the return value is a magic value!

(5).length     // return value: undefined
5 / 0          // return value: Infinity
"cookie" * 10  // return value: NaN

LAB: Values: readings and exercises