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: 1+1=21 + 1 = 2

    • 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:

    • x=2x = 2

    • shoeextcost=99.99shoe ext{ }cost = 99.99

  • 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: 1+1=21 + 1 = 2

    • Math Object:

    • Provides various functions, such as:

      • Math.pow(base,exponent)Math.pow(base, exponent) for powering numbers (e.g., Math.pow(2,53)Math.pow(2, 53)).

      • Math.round(value)Math.round(value) for rounding values.

      • Math.ceil(value)Math.ceil(value) to round up.

      • Math.floor(value)Math.floor(value) to round down.

      • Math.abs(value)Math.abs(value) to return absolute values.

      • Examples of additional methods: Math.max,Math.min,Math.random,Math.PI,Math.EMath.max, Math.min, Math.random, Math.PI, Math.E.

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 extInfinityext{Infinity} or −extInfinity- ext{Infinity}.

    • 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 extInfinityext{Infinity} or −extInfinity- ext{Infinity}.

    • Not a Number (NaN):

    • Occurs when performing invalid arithmetic operations, such as dividing 0/00/0.

    • 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:

    • x=0.3−0.2x = 0.3 - 0.2 and y=0.2−0.1y = 0.2 - 0.1

    • Comparing x==yx == y may yield false due to minute differences.

Working with Dates and Times

  • Creating Date Objects:

    • Example:

    • then=newextDate(2010,0)then = new ext{ }Date(2010, 0) creates a date object for January 2010.

    • now=newextDate()now = new ext{ }Date() creates a date object with the current date.

    • Date Methods:

    • Examples include: getFullYear(),getMonth(),getDate()getFullYear(), getMonth(), getDate().

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:

    • name="Adrian"name = "Adrian"

    • name.lengthname.length 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 a=5a = 5, the expression a==4a == 4 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.