Lecture Notes Of Day 3
Data Types in JavaScript
Objective:
- To
understand and explore the different data types in JavaScript.
- To
learn how to work with both primitive and reference data types.
- To
learn how to check the type of a variable using the typeof operator.
Introduction to Data
Types
In JavaScript, values can
be classified into two main categories:
1. Primitive
Data Types
2. Reference
Data Types
Understanding the
difference between these two is important, as they behave differently in terms
of how they are assigned and copied.
1. Primitive Data
Types
Primitive data types are
the simplest forms of data. They are immutable (unchangeable) and hold a single
value. When you assign a primitive value to a variable, the variable holds the
actual value.
Types of Primitive Data
Types:
1. String:
o A
string represents a sequence of characters enclosed in single quotes ('),
double quotes ("), or backticks (`).
o Strings
are used to represent text.
Examples:
let name = "Alice"; // String using double quotes
let greeting = 'Hello,
world!'; // String using single quotes
let message = `Hello,
${name}`; // String using backticks (template literal)
Key Points:
o Strings
are immutable, meaning you cannot change individual characters of a string once
it is created.
2. Number:
o The Number data
type represents both integer and floating-point numbers.
o JavaScript
does not differentiate between integers and floating-point numbers (they are
all just Number).
Examples:
let age = 25; // Integer number
let price = 9.99;
// Floating-point number
let largeNumber = 1000000;
// Large number
Key Points:
o JavaScript
automatically converts values between integers and floating-point numbers when
necessary.
o Special
values like Infinity, -Infinity, and NaN (Not-a-Number) are
also of type Number.
3. Boolean:
o The Boolean data
type has only two possible values: true or false.
o Booleans
are typically used in conditional statements to control the flow of a program.
Examples:
let isAdult = true; // Boolean value
let isOver18 = false;
Key Points:
o Booleans
are the result of logical operations (like &&, ||, !) or
comparisons (like ==, ===, >, <).
4. Null:
o The null type
represents the intentional absence of any value or object.
o It
is used to indicate that a variable should be empty or undefined.
Examples:
let person = null; // No value is assigned to the variable
Key Points:
o null is
not the same as undefined. null is assigned explicitly,
whereas undefined indicates that a variable has not been assigned a
value.
5. Undefined:
o The undefined type
indicates that a variable has been declared but not assigned a value.
o JavaScript
automatically assigns undefined to variables that are declared but
not initialized.
Examples:
let score; //
Declaration without initialization, so it is undefined
console.log(score);
// undefined
Key Points:
o If
you try to access a property of an object or an array index that does not
exist, it will return undefined.
6. Symbol:
o A Symbol is
a unique and immutable value, often used as an identifier for object
properties.
o Symbols
are particularly useful when you need to create private or unique keys for
objects.
Examples:
let sym1 = Symbol("description");
let sym2 = Symbol("description");
console.log(sym1 ===
sym2); // false (Symbols are unique)
Key Points:
o Symbols
are not convertible to other data types like strings or numbers. They are
unique and immutable.
2. Reference Data
Types
Reference data types are
more complex and include objects that can hold multiple values and different
types of data. When you assign a reference type to a variable, the variable
holds a reference (or memory address) to the actual value.
Types of Reference Data
Types:
1. Object:
o An Object is
a collection of properties, where each property is a key-value pair.
o Objects
are used to store and manage collections of data and functionality.
Examples:
let person = {
name: "John",
age: 30,
isStudent: false
};
console.log(person.name);
// John
Key Points:
o Objects
can hold multiple values of different types, including other objects, arrays,
and functions.
o Properties
can be accessed using dot notation (person.name) or bracket notation (person['name']).
2. Array:
o An Array is
a special type of object used to store ordered collections of data.
o Arrays
can hold multiple values of different types.
Examples:
let colors = ["red",
"green", "blue"];
console.log(colors[0]);
// red
Key Points:
o Arrays
are indexed collections where elements are accessed using zero-based indexes.
o Arrays
are mutable, meaning you can modify, add, or remove elements.
3. Function:
o A Function is
a block of code that can be defined and then invoked (called) when needed.
o Functions
can be assigned to variables, passed as arguments, and returned from other
functions.
Examples:
function greet(name) {
return `Hello,
${name}!`;
}
console.log(greet("Alice"));
// Hello, Alice!
Key Points:
o Functions
in JavaScript are first-class objects, meaning they can be assigned to
variables, passed as arguments, and returned from other functions.
3. Type Checking in
JavaScript
To check the type of a
variable, JavaScript provides the typeof operator. This operator
returns a string indicating the type of the variable.
Syntax:
typeof variable
Examples:
let x = 42;
console.log(typeof x); // "number"
let name = "Alice";
console.log(typeof name); // "string"
let isStudent = true;
console.log(typeof
isStudent); // "boolean"
let obj = { name: "Alice", age: 25 };
console.log(typeof
obj); // "object"
let arr = [1, 2, 3];
console.log(typeof
arr); // "object" (arrays are also objects)
let sym = Symbol();
console.log(typeof
sym); // "symbol"
Key Points:
- The typeof operator
can be used for all data types except null (it returns
"object" for null, which is a known issue in JavaScript).
- It
is useful for checking the type of a variable during debugging and
development.
Summary
- Primitive data types include String, Number, Boolean, Null, Undefined,
and Symbol. These are simple values and immutable.
- Reference
data types include Object, Array,
and Function. These hold collections or more complex structures and
are mutable.
- The typeof operator
is used to check the type of a variable.
Understanding these data
types and how to use them is essential for writing effective and efficient
JavaScript code.
Practice
- Try
declaring variables of each type and print their types using typeof.
- Experiment
with creating objects and arrays, and explore their properties and
methods.
- Use
the typeof operator to identify and debug your code.
