Data types in programming tell us the what kind of information we’re working with.
Here’s how the data types you've listed can be categorized in JavaScript:
A. Primitive Data Types:
--------------------------------
1. Numbers: Used for numbers, whether they are whole or decimal.
Example: age stores the number 30.
let age = 30;
2. Strings: Used for text. Text is always wrapped in quotes.
Example: name stores the text "John".
let name = "John";
3. Boolean: Used for true/false values, like yes/no answers.
Example: isStudent indicates if someone is a student (true or false).
let isStudent = true;
B.Composite(or Reference) Data Types:
----------------------------------------------------
1. Arrays: Used for lists of items, which are put inside square brackets [].
Example: colors holds a list of color names.
let colors = ["red", "green", "blue"];
2. Objects: Used for storing multiple related values and properties in a single
variable,
with key-value pairs inside curly braces {}.
Example: person stores information about a person with firstName and lastName.
let person = {firstName: "Jane", lastName: "Doe"};
Typeof Operator:
----------------
In JavaScript, typeof is an operator used to determine the type of a given variable
or expression.
It returns a string that represents the type of the operand.
Important Note:
--------------------
typeof returns "object" for arrays and null, so checking for arrays or null
requires additional logic.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript</title>
</head>
<body>
<h1>JavaScript typeof Operator</h1>
<h2>To See Console Output, Press F12 Key</h2>
<script>
// Number type
let num = 42;
console.log("Type of num:", typeof num); // "number"
// String type
let str = "Hello, World!";
console.log("Type of str:", typeof str); // "string"
// Boolean type
let isTrue = true;
console.log("Type of isTrue:", typeof isTrue); // "boolean"
// Object type
let obj = { name: "John" };
console.log("Type of obj:", typeof obj); // "object"
// Array type (Arrays are also objects)
let arr = [1, 2, 3];
console.log("Type of arr:", typeof arr); // "object"
// Function type
let func = function() {};
console.log("Type of func:", typeof func); // "function"
// Undefined type
let undefinedVar;
console.log("Type of undefinedVar:", typeof undefinedVar); // "undefined"
// Null type (null is considered an object in JavaScript)
let nullVar = null;
console.log("Type of nullVar:", typeof nullVar); // "object"
</script>
</body>
</html>