0% found this document useful (0 votes)
15K views

Chapter 1

The document summarizes key concepts about values, types, and operators in JavaScript. It discusses the number, string, Boolean, and empty value types. It covers arithmetic, comparison, logical, and unary operators. It also describes automatic type conversion that can occur when operators are applied to values of different types, and how logical operators handle values through short-circuit evaluation.

Uploaded by

Kulmie Pro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15K views

Chapter 1

The document summarizes key concepts about values, types, and operators in JavaScript. It discusses the number, string, Boolean, and empty value types. It covers arithmetic, comparison, logical, and unary operators. It also describes automatic type conversion that can occur when operators are applied to values of different types, and how logical operators handle values through short-circuit evaluation.

Uploaded by

Kulmie Pro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

1

Chapter 1
Values, Types and Operators
Values 2

Imagine a sea of bits—an ocean of them.


A typical modern computer has more than 30 billion bits in its volatile data
storage (working memory). Nonvolatile storage (the hard disk or equivalent)
tends to have yet a few orders of magnitude more.
To be able to work with such quantities of bits without getting lost, we must
separate them into chunks that represent pieces of information.
3

In a JavaScript environment, those chunks are called values. Though


all values are made of bits, they play different roles. Every value has a
type that determines its role.
Some values are numbers, some values are pieces of text, some values
are functions, and so on.
To create a value, you must merely invoke its name.
4
Numbers
Values of the number type are, unsurprisingly, numeric values. In a
JavaScript program, they are written as follows:
13
5
Arithmetic
The main thing to do with numbers is arithmetic. Arithmetic operations
such
as addition or multiplication take two number values and produce a
new number
from them. Here is what they look like in JavaScript:
100 + 4 * 11.
The + and * symbols are called operators.
6

The % symbol is used to represent the remainder operation. X % Y


is the remainder of dividing X by Y. For example, 314 % 100 produces
14, and 144 % 12 gives 0. The remainder operator’s precedence is the
same as that of multiplication and division. You’ll also often see this
operator referred to as modulo.
7
Special numbers

There are three special values in JavaScript that are considered


numbers but don’t behave like normal numbers.
The first two are Infinity and -Infinity, which represent the positive and
negative infinities.
Infinity - 1 is still Infinity, and so on. Don’t put too
much trust in infinity-based computation, though.
8

It isn’t mathematically sound, and it will quickly lead to the next special
number: NaN.
NaN stands for “not a number”, even though it is a value of the number
type.
You’ll get this result when you, for example, try to calculate 0 / 0 (zero
divided by zero), Infinity - Infinity, or any number of other numeric
operations that don’t yield a meaningful result.
9
Strings
The next basic data type is the string. Strings are used to represent text. They are
written by enclosing their content in quotes.
`Down on the sea`
"Lie on the ocean“
’ Float on the ocean'
You can use single quotes, double quotes, or backticks to mark strings, as long as
the quotes at the start and the end of the string match.
10
Cont.…
Almost anything can be put between quotes, and JavaScript will make a string value
out of it. But a few characters are more difficult.
You can imagine how putting quotes between quotes might be hard. Newlines (the
characters you get when you press enter) can be included without escaping only when
the string is quoted with backticks (\‘).
To make it possible to include such characters in a string, the following
notation is used: whenever a backslash (\) is found inside quoted text, it
indicates that the character after it has a special meaning. This is called
escaping the character. A quote that is preceded by a backslash will not end the string
but be part of it.
Cont.… 11

When an n character occurs after a backslash, it is interpreted as a newline.


Similarly, a t after a backslash means a tab character.
Take the following string:
"This is the first line\nAnd this is the second"
The actual text contained is this:
This is the first line
And this is the second
12
Cont.…
Strings cannot be divided, multiplied, or subtracted, but the + operator
can be used on them. It does not add, but it concatenates—it glues two
strings together.
The following line will produce the string "concatenate":
"con" + "cat" + "e" + "nate“.
Cont.… 13

Backtick-quoted strings, usually called template literals, can do a few


more tricks.
Apart from being able to span lines, they can also embed other values.
`half of 100 is ${100 / 2}`
When you write something inside ${ } in a template literal, its result will
be computed, converted to a string, and included at that position.
The example produces “half of 100 is 50”.
Unary operators 14

Not all operators are symbols. Some are written as words. One
example is the typeof operator, which produces a string value
naming the type of the value you give it.
console.log(typeof 4.5)
// → number
console.log(typeof "x")
// → string
Cont.… 15

The other operators shown all operated on two values, but typeof
takes only one.
Operators that use two values are called binary operators, while
those that take one are called unary operators. The minus operator
can be used both as a binary operator and as a unary operator.
console.log(- (10 - 2))
// → -8
Boolean values 16

It is often useful to have a value that distinguishes between only two


possibilities, like “yes” and “no” or “on” and “off”.
For this purpose, JavaScript has a Boolean type, which has just two
values, true and false, which are written as those words.
Comparison 17

Here is one way to produce Boolean values:


console.log(3 > 2)
// → true
console.log(3 < 2)
// → false
The > and < signs are the traditional symbols for “is greater than” and “is
less than”, respectively

Strings can be compared in the same way.


console.log("Aardvark" < "Zoroaster")
// → true
Cont.… 18

When comparing strings, JavaScript goes over the characters from left to right, comparing
the Unicode codes one by one.
Other similar operators are >= (greater than or equal to), <= (less than or equal to), ==
(equal to), and != (not equal to).
console.log("Itchy" != "Scratchy")
// → true
console.log("Apple" == "Orange")
// → false.

There is only one value in JavaScript that is not equal to itself, and that is
NaN (“not a number”).
console.log(NaN == NaN)
// → false
Logical operators 19

There are also some operations that can be applied to Boolean values
themselves.
JavaScript supports three logical operators: and, or, and not.
These can be used to “reason” about Booleans.
The && operator represents logical and. It is a binary operator, and its
result is true only if both the values given to it are true.
console.log(true && false)
// → false
console.log(true && true)
// → true
20

The || operator denotes logical or. It produces true if either of the


values given to it is true.
console.log(false || true)
// → true
console.log(false || false)
// → false
Cont.… 21

Not is written as an exclamation mark (!). It is a unary operator that flips


the value given to it—!true produces false, and !false gives true.
The last logical operator I will discuss is not unary, not binary, but ternary,
operating on three values. It is written with a question mark and a colon, like
this:

console.log(true ? 1 : 2);
// → 1
console.log(false ? 1 : 2);
// → 2
Empty values 22

There are two special values, written null and undefined, that are
used to denote the absence of a meaningful value.
They are themselves values, but they carry no information.
Automatic type conversion 23
In the Introduction, I mentioned that JavaScript goes out of its way to accept
almost any program you give it, even programs that do odd things.
This is nicely demonstrated by the following expressions:
console.log(8 * null)
// → 0
console.log("5" - 1)
// → 4
console.log("5" + 1)
// → 51
console.log("five" * 2)
 // → NaN
 console.log(false == 0)
 // → true
Cont.… 24

However, when null or undefined occurs on either side of the operator,


it produces true only if both sides are one of null or undefined.
console.log(null == undefined);
// → true
console.log(null == 0);
// → false
Short-circuiting of logical operators 25

The logical operators && and || handle values of different types in a peculiar way.
They will convert the value on their left side to Boolean type in order to decide
what to do, but depending on the operator and the result of that conversion, they
will return either the original left-hand value or the righthand value.

This has the expected effect when the values are Boolean and does something
analogous for values of other types.
console.log(null || "user")
// → user
console.log("Agnes" || "user")
// → Agnes
Summary 26
We looked at four types of JavaScript values in this chapter: numbers, strings, Booleans, and undefined
values.
Such values are created by typing in their name (true, null) or value (13
, "abc"). You can combine and transform values with operators.
We saw binary operators for arithmetic (+, -, *, /, and %), string concatenation (+),
comparison (==, !=, ===, !==, <, >, <=, >=), and logic (&&, ||), as well as several
unary operators (- to negate a number, ! to negate logically, and typeof to
find a value’s type) and a ternary operator (?:) to pick one of two values based
on a third value.
This gives you enough information to use JavaScript as a pocket calculator
but not much more.
The next chapter will start tying these expressions together
into basic programs.
27

END

You might also like