----------
2 Programming Basics
In the last chapter, we introduced JavaScript, then set up a
programming environment. We even got our hands dirty with a few JavaScript
programs. In this chapter, we’ll delve further and learn how JavaScript works,
as well as write some more programs.
This chapter will cover the following topics:
– The importance of well-commented code
– JavaScript grammar ― expressions, statements, semicolons
and whitespace
– Primitive data types
– Strings ― string literals, string properties and methods
– Declaring and assigning constants and variables
– Numbers ― decimal, hexadecimal, octal, binary and exponent
form, Infinity,
and NaN
– Arithmetic operations such as +, -, *, /, and %
– Undefined and null
– Booleans ― truthy and falsy values
– Logical operators ― AND, OR, and NOT
– Our project ― we’ll set some question-and-answer variables
and use alert boxes to display them
2.1
Comments (p32)
2.2
JavaScript Grammar (p33)
2.3
Reserved Word (p34)
2.4
Primitive data type (p35)
JavaScript has seven different data types. Six of them are
primitive data types and are listed below:
– String
– Symbol
– Number
– Boolean
– Undefined
– Null
Any value that isn’t one of the primitive data types listed
above is an object. These include arrays, functions and object literals.
JavaScript has a special operator called typeof for finding
out the type of a value.
Here are some examples of the different value types:
typeof 'hello'
<< 'string'
typeof 10
<< 'number'
typeof true
<< 'boolean'
typeof { ninja: 'turtle' }
<< 'object'
typeof [ 1, 2, 3 ]
<< 'object'
2.5
Variables (p38)
Variables are used in programming languages to refer to a
value stored in memory.
Variables have to be declared before they can be used. From
ES6 onwards, JavaScript uses the keywords const and let to declare variables.
The keyword const is used when the variable will not be reassigned to another
value, whereas let is used if the variable might be reassigned later in the
program.
What happened to var?
In versions of JavaScript previous to ES6, variables were
declared using the keyword var. The following code shows how a variable called
number would be declared and assigned the value of 2:
var number = 2;
This worked in much the same way as using let. The main
difference was that variables declared using let and const have block scope,
which is discussed in more detail below. They also prevent you from overwriting
any built-in methods by assignment, which is generally frowned upon, whereas
using var doesn’t.
So why was the new word let introduced into ES6? Why not
just change the behavior of var?
Remember that a core tenet of the JavaScript language is
that it has to remain backwardly compatible. This means that the behavior of
var had to remain consistent, so couldn’t just be changed in ES6. For that
reason, the new keyword let was introduced.
You should be aware of var though, as you will see it used
frequently in older code examples around the web to declare variables.
2.6
Scope (p40)
2.7
Naming Constants & Variables (p43)
2.8
Direct Assignment and Assignment By Reference
(p44)
2.9 String (p45)
2.10 Template
Literals (p52)
2.11 Symbol
(p53)
2.12 Numbers
(p55)
2.13 Number
Methods (p57)
2.14 Changing
The Value of Variables (p60)
2.15 Type
Coercion (p64)
2.16 Converting
Between Strings and Numbers (p65)
2.17 Undefined
and NULL (p69)
2.18 Booleans
(p70)
2.19 Logical
Operators (p71)
2.20 Bitwise
Operators (p74)
2.21 Comparison
(p78)
2.22 Labs:
Lab 4. Quiz Ninja Project (p84)
-----
Cập nhật: 17/10/2019
-----