Boolean: Represents logical values: true or false.
Undefined: Represents a variable that has been declared but not assigned a value.
Null: Represents an intentional absence of any object value.
BigInt: Used for integers too large for the Number type.
Symbol: Represents a unique identifier.
let name = "JavaScript"; // String
let age = 25; // Number
let isActive = true; // Boolean
let x; // Undefined
let y = null; // Null
let bigVal = 123456789n; // BigInt
Output will appear here...
2. Non-Primitive Data Types
Non-primitive data types (Reference types) are objects. They can hold collections of values and more complex
entities.
Object: A collection of key-value pairs.
Array: An ordered list of values.
Function: A block of code designed to perform a task.
// Object
let student = {
name: "Ravi",
age: 20
};
// Array
let colors = ["Red", "Green", "Blue"];
// Function
function greet() {
return "Hello";
}
Output will appear here...
Type of Operator
The typeof operator returns a string indicating the data type of a variable or expression.
typeof "John" returns "string"
typeof 3.14 returns "number"
typeof true returns "boolean"
typeof [1,2,3] returns "object" (Arrays are objects)
Q1: Declare variables with different primitive data types and print their types.
A string for a name
A number for an age
A boolean for student status
You can declare the variables using let or const, and use the typeof operator to check their data types.
let name = "Ragini";
let age = 20;
let isStudent = true;
document.write("Name type:", typeof name);
document.write("Age type:", typeof age);
document.write("Student status type:", typeof isStudent);
Easy
Q2: Identify the data type of an unassigned variable.
Declare a variable without assigning any value to it.
Print the variable itself.
Print the type of the variable using typeof.
When a variable is declared but not assigned a value, both its value and its data type are undefined.
let myVar;
document.write("Value:", myVar);
document.write("Type:", typeof myVar);
Medium
Q3: Demonstrate the difference between null and undefined.
Create a variable naturally assigned to undefined.
Create another variable explicitly assigned to null.
Print the typeof for both.
undefined means no value has been assigned yet. null is an intentional assignment representing "no value". Interestingly, typeof null returns "object" due to a known bug in early JavaScript.
let notAssigned;
let emptyValue = null;
document.write(typeof notAssigned); // "undefined"
document.write(typeof emptyValue); // "object"
Medium
Q4: Create an object and check the data types of its properties.
Create a user object with properties: name (string) and age (number).
Check and print the data type of the object itself.
Check and print the data type of user.name and user.age.
The object itself is of type "object", while its individual properties will return their respective primitive types ("string" and "number").
Q5: Write a program to verify if a variable is an array.
Declare an array of numbers: [10, 20, 30].
Print the result of typeof on this array to see what it returns.
Write the correct JavaScript method to definitively verify it is an array.
Because arrays are structurally a type of object in JavaScript, typeof will return "object". To correctly identify an array, use the built-in Array.isArray() method.
let arr = [10, 20, 30];
// This returns "object", which can be confusing!
document.write("Using typeof:", typeof arr);
// This correctly returns true
document.write("Is it an array?:", Array.isArray(arr));