C: : Types, Values, and Variables (part 1)
Chapter Three: Types, Values, and Variables
General Overview of Types
Types refer to the kinds of values that something can take on and the operations that can be performed on these values in programming.
Operations on Different Types
Numbers
Operations: Add (+), Subtract (-), Multiply (*), Divide (/)
Example: If
x = 2, thenx + 3 = 5
Strings
Defined as a series of characters.
Example: The string "one" and the string "two" can be concatenated using the plus symbol (+) to become "onetwo".
Concatenation: The process of joining strings together.
Variables
Definition: Variables are named references used to store values for later use in a program.
Examples of Variable Assignments:
x = 2shoe_cost = 99.99
Use Cases for Variables:
Storing user input.
Assigning constants for later use (e.g., tax rate).
Types of Groupings in JavaScript
JavaScript categorizes types into primitive types and object types.
Primitive Types:
Includes:
Numbers
Strings
Booleans
Null
Undefined
Object Types:
Everything that is not a primitive type, allowing for holding multiple values, methods, and attributes.
Memory Management
Garbage Collection
A mechanism that automatically manages memory allocation and deallocation, freeing memory that is no longer in use.
In contrast to older languages, where programmers had to manage memory manually, JavaScript does it automatically.
Working with Numbers in JavaScript
Type of Numbers:
JavaScript uses a unified number type, which is a 64-bit floating point.
This means that both integers and floating points are treated as floating points.
Arithmetic Operations Example:
1 + 1equals2.
Math Object Functions
Functions available in the Math object include:
Math.pow(base, exponent)- Raises base to an exponent.Example:
Math.pow(2, 53)gives a large number.Math.round(value)- Rounds to nearest integer.Math.ceil(value)- Rounds upwards to nearest integer.Math.floor(value)- Rounds downwards to nearest integer.Math.abs(value)- Gets the absolute value.Common properties:
Math.PI,Math.E.
Special Numerical Cases
Overflow and Underflow
Overflow: Occurs when a variable exceeds its limit, resulting in the value being represented as infinity.
Underflow: Occurs when a number becomes too close to zero, which can result in the default value of zero.
Division by Zero:
In JavaScript, dividing by zero produces
Infinityor-Infinity, depending on the sign of the number.Dividing zero by zero results in
NaN(Not a Number).
NaN Characteristics:
NaNis not equal to anything, including itself.
Binary Floating Point Representation
Concept: Numbers in computers are represented as binary floating-point numbers, which can lead to precision issues and rounding errors.
Rounding Errors: Comparing floating point values directly can lead to unexpected results due to small inaccuracies in representation.
Working with Dates and Times
Date Creation: Using the
new Date()constructor.Example:
var then = new Date(2010, 0, 1);creates a date object for January 1, 2010.Example:
var now = new Date();retrieves the current date and time.
Date Methods:
getFullYear(),getMonth(),getDate()for retrieving respective date components.
Understanding Strings in JavaScript
Strings are defined as immutable sequences of characters.
Once a string is created, it cannot be changed; any modification results in a new string.
String Properties:
Length Property:
name.lengthreturns the number of characters in the string.Example: If
var name = "Adrian";, thenname.lengthreturns6.
String Literals:
Can be single or double-quoted (e.g.,
"Hello World!").Escape Sequences: Used to include special characters in strings.
Example:
"This is a string with a newline character:\nNext Line!"
Boolean Values
Boolean values only return
trueorfalse.Example:
5 == 5evaluates totrue, while5 == 4evaluates tofalse.
Usage in Conditional Statements:
Commonly used in conditionals (e.g.,
ifstatements) to control program flow based on logical comparisons.
Conclusion of Lecture
This lecture has introduced fundamental concepts surrounding types, values, and variables in JavaScript, providing a foundation for deeper exploration in subsequent lectures.