| JavaScript Variables and Data Types | Javascript-Operators | |
JavaScript Data Types |
In this lesson, you’ll learn:
In JavaScript, Primitive Types are the most basic data types. They represent single values (not collections) and are immutable (cannot be changed directly).
A sequence of characters enclosed in quotes.
let name = "Alice"; console.log(name); // "Alice"
Represents both integers and floating-point numbers.
let age = 25; let price = 99.99; console.log(age); // 25 console.log(price); // 99.99
Represents logical values: true or false.
let isStudent = true; let isAdmin = false; console.log(isStudent); // true
A variable declared but not assigned any value is undefined.
let city; console.log(city); // undefined
Represents an intentional absence of any value.
let car = null; console.log(car); // null
Used to create unique identifiers.
let id1 = Symbol("id");
let id2 = Symbol("id");
console.log(id1 === id2); // false (always unique)
Used for very large integers beyond the safe limit of Number.
let bigNum = 123456789012345678901234567890n; console.log(bigNum);
An Object in JavaScript is a collection of key-value pairs. Keys are called properties, and values can be any data type (string, number, array, function, etc.).
// Example of an object
let user = {
name: "Alice",
age: 25,
isStudent: true
};
console.log(user.name); // Alice
console.log(user.age); // 25
An Array is an ordered list of values. Each value is called an element, and each element has an index (starting from 0).
// Example of an array let fruits = ["apple", "banana", "cherry"]; console.log(fruits[0]); // apple console.log(fruits[2]); // cherry
A Function is a block of code designed to perform a task. Functions can take inputs (parameters) and return outputs (values).
// Example of a function
function greet(name) {
return "Hello, " + name + "!";
}
console.log(greet("Alice")); // Hello, Alice!
There are two main types of conversion:
== because it does coercion. Prefer === for strict comparison.JavaScript automatically converts one data type to another when needed.
// String + Number → String
console.log("5" + 2); // "52"
// String - Number → Number
console.log("5" - 2); // 3
// Boolean + Number → Number
console.log(true + 1); // 2
// Equality operator (==) does coercion
console.log(5 == "5"); // true
We manually convert values using built-in functions.
console.log(Number("123")); // 123
console.log(parseInt("12.34")); // 12
console.log(parseFloat("12.34"));// 12.34
console.log(+"5"); // 5 (unary plus)
console.log(String(123)); // "123" console.log((123).toString()); // "123" console.log(true.toString()); // "true"
console.log(Boolean(1)); // true
console.log(Boolean(0)); // false
console.log(Boolean("Hello")); // true
console.log(Boolean("")); // false
// Null and Undefined
console.log(Number(null)); // 0
console.log(Number(undefined)); // NaN
// Empty string
console.log(Number("")); // 0
// Non-numeric string
console.log(Number("abc")); // NaN
In the next lesson, we’ll learn about Operators in JavaScript (Arithmetic, Comparison, Logical).
| JavaScript Variables and Data Types | Javascript-Operators | |