JavaScript provides different data types to store and manipulate data. These types include Numbers, Strings, Booleans, Null, Undefined, Objects, and Arrays.
let num1 = 10;
let num2 = 5.7;
let largeNumber = 5e7; // 5 * 10^7
2. String Data Type
A string is a sequence of characters enclosed in quotes.
JavaScript treats text values as strings.
Single (') or double (") quotes can be used.
Examples:
let firstName = "John";
let greeting = 'Hello, World!';
let message = "JavaScript is fun!";
3. Boolean Data Type
A boolean can have only two values: true or false.
Commonly used in conditional statements.
Examples:
let isLoggedIn = true;
let hasAccess = false;
4. Null Data Type
null represents an empty or unknown value.
It is not the same as 0 or undefined.
Example:
let emptyValue = null;
5. Undefined Data Type
A variable is undefined when it has been declared but not assigned a value.
Example:
let myVar;
console.log(myVar); // Output: undefined
6. Object Data Type
Objects store key-value pairs and are used to group related data together.
Example:
let person = {
name: "Alice",
age: 25,
isStudent: false
};
console.log(person.name); // Output: Alice
7. Array Data Type
An array is a special type of object that holds a list of values.
Example:
let colors = ["Red", "Green", "Blue"];
console.log(colors[0]); // Output: Red
**Bonus**
JavaScript Arithmetic
JavaScript can perform arithmetic operations on variables, just like algebra.
1. Basic Arithmetic
Example:
let x = 10;
let y = x - 5; // Subtraction
let z = y + 5; // Addition
console.log(z); // Output: 10
2. Common Operators
| **Operator** | **Description** | **Example** |
|-------------|--------------------------|----------------|
| `+` | Addition | `x + y` |
| `-` | Subtraction | `x - y` |
| `*` | Multiplication | `x * y` |
| `/` | Division | `x / y` |
| `%` | Modulus (Remainder) | `x % y` |
| `++` | Increment | `x++` or `++x` |
| `--` | Decrement | `x--` or `--x` |
š” **Note:**
- `++x` (prefix increment) increases the value before using it.
- `x++` (postfix increment) uses the value first, then increases it.
- `/` returns a **quotient**, while `%` returns the **remainder**.
š” Bonus Tip:
š Order of Operations: JavaScript follows the standard operator precedence rules (Parentheses, Multiplication/Division, Addition/Subtraction).
šØ Styling for Readability:
ā Use bold text for data type names and key concepts.
ā Add comments (//) to explain code examples.
ā Use tables for structured information like operators.
ā Apply consistent formatting for examples.
To mark this module as complete, you must finish this quiz. Once submitted, you'll need to wait 2 hours before attempting it again.