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.)
A number is what it sounds like -- any integer or decimal.
10 -12 3.14
Lab: Playing with Numbers
Open up a node environment in your terminal (
* Add two numbers together with the `+` operator * Multiply two numbers with the `*` operator * Divide two numbers using the `/` operator * Square a number by raising it to the power of 2 `**2` * Create a more complex mathematical equation. Useing a mixture of different operators * Bonus: Weird math! Try the following equations: * `0.1 + 0.2`, `2**53` and `2**53 + 1`, and `Infinity - Infinity`
A string is an object that's a collection of characters, like a word or a sentence.
"apple" "banana" "Cherry Pie"
Slicing and Dicing
Every string is made of lots of other strings.
You can pull out parts of a string with the
// this means "slice from character 0 to character 4" "blueberry".slice(0, 4) // this means "slice from character 4 to the end "blueberry".slice(4)
These start and end numbers are called indexes (or indices if you're feeling fancy).
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,
- includes the character to the right of the start index
- includes the character to the left of the end index...
- ...but excludes the character to the right of the end index
Try various start and end values in the console and see what happens!
Q: A string is "a series of characters"... but what is a character?
A: a character is a number (or character code) that stands for a symbol.
(Some characters stand for unprintable symbols like
Lab: Playing with Strings
Open up a Node instance in your terminal, and let's play around with strings!
* Add two strings together that use the same type of quotes (e.g.`'Java' + 'Script'`) * Add two strings together using different types of quotes (e.g. `"I'm double quoted!" + 'I am single quoted.') * Subtract two strings (e.g. `'cats' - 'dogs'`) * Add a string to a number
What happens when you try to put quotes inside your string's quotes?
Lab: String Methods
Strings have many operations attached to them. These attached operations are refered to as 'methods' (more on that later) and are quite useful. Let's try a few of them out in your console now:
"titanic".toUpperCase() "QUIETLY".toLowerCase() "Java".repeat(10) "banana".length "berry".charAt(1) "berry".charAt(0) "apple" "banana".includes("nan") "banana".endsWith("ana") "blueberry".replace("blue", "black")
Values can be combined or manipulated using operators, like...
- PLUS (
- TIMES (
- POWER (
- DOT (
- ASSIGNMENT (
- COMPARISON (
An operator sends a message to the value
1 + 2sends the number
please add 2 to yourself.
Dot is a special operator that sends arbitrary messages; we will learn more about her later.
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".
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 special value!
(5).length // return value: undefined 5 / 0 // return value: Infinity "cookie" * 10 // return value: NaN
Expression vs Statement Breakdown
|has a value||does something|
|can be assigned to a var||has a semicolon (optionally)|
Readings and Exercises: