What are Data Types?
Data types refer to the Type
of data such as text, numbers, true/false values, etc.
1. Number
Represents numeric values, including integers, floating-point numbers, and natural numbers.
Example:
let age = 16;
let height = 5.9;
2. String
Strings represent textual information. Any letters, words, or sentences enclosed in quotes (' ', " ") are considered strings.
Example:
let name = 'John';
let message = 'Hello, there!';
3. Boolean
The Boolean data type can only hold one of two values: true or false. It's like turning a switch on and off.
Example:
let isStudent = true;
let hasDriverLicense = false;
4. Object
Objects are used to store multiple values in a single variable. For example, a student object can hold name, age, and grade information.
Example:
let student = {
name: 'John',
age: 16,
grade: 'Sophomore',
};
5. Array
Arrays are used to store multiple values of the same type in a single variable.
Example:
let fruits = ['Apple', 'Banana', 'Grapes'];
6. Other Data Types
null
null
is a special value that represents 'no value'. It is used to explicitly indicate that a variable should have no value.
Example:
let emptyValue = null;
undefined
undefined
is used to indicate that a variable has been declared but has not yet been assigned a value. It means 'not defined'. When a variable is declared without initialization, JavaScript assigns it a value of undefined
.
Example:
let notDefinedYet;
console.log(notDefinedYet); // Output: undefined
null
andundefined
both represent the concept of 'no value', but they are used in different contexts and have different meanings.null
is used when you want to explicitly indicate that a variable has no value, whereasundefined
indicates that a variable has not been initialized or a value has not been assigned where it was expected.
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.