JavaScript supports several data types that are used to represent different kinds of values. Here's an overview of the main data types in JavaScript:
Primitive Data Types:
a. Number:
- Represents numeric values, including integers and floating-point numbers.
javascript var age = 30; var price = 19.99;
b. String:
- Represents textual data enclosed within single ('') or double ("") quotes.
javascript var name = "John"; var message = 'Hello, world!';
c. Boolean:
- Represents logical values true
or false
.
javascript var isFound = true; var isExpired = false;
d. Null:
- Represents an intentional absence of any object value.
javascript var emptyValue = null;
e. Undefined:
- Represents a variable that has been declared but has not been assigned a value yet.
javascript var notDefined;
f. Symbol (added in ECMAScript 6): - Represents a unique identifier that is used to avoid naming conflicts.
Non-Primitive Data Types:
a. Object:
- Represents a collection of key-value pairs where keys are strings and values can be of any data type, including other objects.
javascript var person = { name: "John", age: 30, isStudent: false };
b. Array:
- Represents an ordered list of values, where each value can be of any data type, including other arrays and objects.
javascript var numbers = [1, 2, 3, 4, 5]; var fruits = ["apple", "banana", "orange"];
Type Conversion:
javascriptvar num = 10;
var str = "20";
var result = num + str; // Result: "1020"
Type Checking:
typeof
operator to check the data type of a variable or expression.javascriptvar num = 10;
var type = typeof num; // Result: "number"
Understanding JavaScript data types is crucial for writing efficient and bug-free code. It's essential to know how to work with different data types and how they behave in various contexts.
Thanks for learning