E: Types, Values, and Variables (part 3)
Chapter Three: Types, Values, and Variables
General Overview
This chapter is divided into a series of lectures.
Focus is on understanding types, values, and variables in JavaScript.
Types in JavaScript
Definition of Types: Types refer to the kind of values that something can take on and what operations can be performed on those values.
Types include: Different operations associated with each type, especially numbers and strings.
Types of Values
Numbers:
Operations: Can be added, subtracted, multiplied, and divided.
Examples include:
Addition:
Subtraction, multiplication, and division operation representations are similar.
Strings:
Definition: A series of characters.
Example: "one" and "two" (as strings).
Concatenation:
Using the plus symbol (+) to join strings:
Example: "one" + "two" results in "onetwo".
Using Variables
Definition of Variables: A variable is a container for a value that can be used later in code.
Examples of Variable Assignments:
Purpose: To remember values, such as user inputs or constant values (e.g., county tax at 5%).
Variable Declarations
Discusses how variables are declared and defined in JavaScript.
Grouping of Objects
Types of Groupings:
Primitive Types:
Components:
Numbers
Strings
Booleans
Null
Undefined
Object Types:
Definition: Everything that is not a primitive type.
Structure: Can include arrays and other complex types with properties and methods.
Memory Management
Garbage Collection:
Importance: Manages memory by reclaiming space taken up by unreachable objects in JavaScript.
Comparison with Other Languages: Unlike languages like C, programmers in JavaScript do not manually manage memory.
Process:
Periodically checks objects that are no longer referenced and frees that memory.
Numbers in JavaScript
Distinction of Numbers:
JavaScript treats all numbers as floating-point numbers.
Floating-Point Representation:
Specifically, 64-bit floating-point numbers.
Examples:
Can represent integers as well as decimals without distinction.
Arithmetic Operations
Operations available include:
Addition:
Math Object:
Provides various functions, such as:
for powering numbers (e.g., ).
for rounding values.
to round up.
to round down.
to return absolute values.
Examples of additional methods: .
Error Conditions in JavaScript
Underflow and Overflow:
An overflow occurs when trying to assign a number that is too large for storage in a variable.
Results in a special value, either or .
Underflow:
Happens when a value gets too close to zero and might be rounded to zero.
Division Scenarios
Dividing by Zero:
JavaScript allows division by zero, resulting in or .
Not a Number (NaN):
Occurs when performing invalid arithmetic operations, such as dividing .
Notable characteristic: NaN is not equal to any value, including itself.
Representation of Numbers in Computing
Binary Floating Point:
Numbers in computers are represented using binary floating points.
Limitations exist where some numbers cannot be represented accurately, leading to rounding errors.
Important when comparing decimal values due to potential inaccuracies.
Example of Rounding Errors
Variable Declaration with Rounding Errors:
Example:
and
Comparing may yield false due to minute differences.
Working with Dates and Times
Creating Date Objects:
Example:
creates a date object for January 2010.
creates a date object with the current date.
Date Methods:
Examples include: .
Strings in JavaScript
Definition of Strings:
A string is a series of characters, defined as immutable (cannot be changed once created).
String Length:
Property: Length returns the number of characters in a string.
Example:
returns 6.
Empty String:
Defined as a string of length 0.
String Literals and Quotes
Usage of Quotes:
"string"vs"string"allows for flexibility in including quotes inside strings.Escape Characters:
Defined with backslash () before a character to include special characters (e.g., new line sequence: \n).
Boolean Values
Definition: Boolean values can only be true or false.
Usage in Conditions:
Comparison uses operators such as == (equality).
Example:
If , the expression returns false.
Typically used: Conditional statements, like if conditions and while loops.
Conclusion
This concludes the first lecture on Chapter Three.
The next lectures will delve deeper into variables and objects.