5. Values, Types, and Operators#
5.1. Variable declare#
In JavaScript, you can introduce variables using various keywords such as var
, let
, and const
5.1.1. var
#
Function Scoped
: Variables declared withvar
are function-scoped. This means that they are accessible throughout the function in which they are declared, regardless of block scope.Hoisting
: Variables declared withvar
are hoisted to the top of their function or global scope. This means you can access the variable before it’s declared without causing an error, although its value will beundefined
.Re-assignable
: You can reassign values to variables declared withvar
.
5.1.2. let
#
Block Scoped
: Variables declared withlet
are block-scoped, meaning they are only accessible within the block they are defined in (e.g., within loops, conditionals, or functions).No Hoisting
: Unlikevar
, variables declared withlet
are not hoisted to the top of their scope. They are only accessible after they are declared.Re-assignable
: Variables declared withlet
can be reassigned new values.
* Preferred Modern Choice
: let
is the preferred choice for variable declaration in modern JavaScript due to its block scope and lack of hoisting.
5.1.3. const
#
Block Scoped
: Likelet
, variables declared withconst
are block-scoped.No Hoisting
: Similar tolet
,const
variables are not hoisted.Immutable
: Variables declared withconst
cannot be reassigned once they are initialized. However, this does not mean the value itself is immutable; if the value is anobject
orarray
, its properties or elements can still be modified.
// Declaring a variable using var
var message = "Hello, World!";
console.log(message); // Output: Hello, World!
// Declaring a variable using let
let count = 10;
console.log(count); // Output: 10
// Declaring a constant variable using const
const PI = 3.14;
console.log(PI); // Output: 3.14
5.2. Values#
Every
value
has atype
that determines its roleSome
values
arenumbers
, somevalues
are pieces oftext
, somevalues
arefunctions
, and so on.
typeof 42; // Outputs: "number" typeof "Hello"; // Outputs: "string" typeof true; // Outputs: "boolean" typeof undefined; // Outputs: "undefined" typeof null; // Outputs: "object" (which is a known quirk) typeof {}; // Outputs: "object" typeof []; // Outputs: "object" (arrays are also objects) typeof function () {}; // Outputs: "function"
5.3. Numbers#
In a JavaScript program, they are written as follows :
13
For very big or very small numbers, you may also use scientific notation by adding an
e
(for exponent), followed by the exponent of the number.2.998e8
Special numbers: There are three special values in JavaScript that are considered
numbers
but don’t behave like normalnumbers
.The first two are
Infinity
and-Infinity
Third is
NaN
, stands for “not a number”
JavaScript uses a fixed number of bits, 64 of them, to store a single
number
value.The important thing is to be aware of it and treat fractional digital numbers as approximations, not as precise values.
5.4. Arithmetic operator#
Addition (
+
): Adds two operands.let sum = 3 + 5; // sum will be 8
Subtraction (
-
): Subtracts the right operand from the left operand.let difference = 10 - 4; // difference will be 6
Multiplication (
*
): Multiplies two operands.let product = 2 * 6; // product will be 12
Division (
/
): Divides the left operand by the right operand.let quotient = 8 / 2; // quotient will be 4
Remainder/Modulus (
%
): Returns the remainder when the left operand is divided by the right operand.let remainder = 10 % 3; // remainder will be 1
Exponentiation (
**
): Raises the left operand to the power of the right operand.let result = 2 ** 3; // result will be 8
5.5. String#
Strings
cannot be divided, multiplied, or subtracted, but the+
operator can be used on them. It does not add, but itconcatenates
—it glues two strings together."con" + "cat" + "e" + "nate";
Backtick-quoted strings,
${}
, usually calledtemplate literals
. its result will be computed, converted to a string, and included at that position.`half of 100 is ${100 / 2}`; // half of 100 is 50
5.6. Logical Operators#
||
has the lowest precedence, then comes &&
, then the comparison operators (>
, ==
, and so on), and then the rest.
Logical AND (&&)
: This operator returns true if both operands are true; otherwise, it returns false.let x = true; let y = false; console.log(x && y); // Outputs: false
Logical OR (||)
: This operator returns true if at least one of the operands is true; otherwise, it returns false.let a = true; let b = false; console.log(a || b); // Outputs: true
Logical NOT (!)
: This operator returns the opposite boolean value of the operand. If the operand is true, it returns false, and if the operand is false, it returns true.let p = true; console.log(!p); // Outputs: false let q = false; console.log(!q); // Outputs: true
Ternary operator: It’s often used as a shorthand for simple
if...else
statements.condition ? expression1 : expression2
let age = 20; let message = age >= 18 ? "You are an adult" : "You are a minor"; console.log(message); // Outputs: "You are an adult"
5.7. Empty values#
There are two special values, written null
and undefined
, that are used to denote the absence of a meaningful value.
Many operations in the language that don’t produce a meaningful value yield
undefined
simply because they have to yield some value.
5.8. Automatic type conversion#
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
console.log(null == undefined);
// → true
console.log(null == 0);
// → false
console.log(null || "user");
// → user
console.log("Agnes" || "user");
// → Agnes