JavaScript Data Types
Data types in JavaScript are used to define the type of data that can be stored in a variable or constant. JavaScript has several built-in data types, each serving a specific purpose. Let's explore the most common ones.
Why Data Types are Used
Data types are important in JavaScript for several reasons:
They define how data is stored and processed. They help prevent unexpected type-related errors. They enable the interpreter to allocate memory and optimize code execution.
Number
The Number data type represents both integer and floating-point numbers. It is used for mathematical operations, such as addition, subtraction, multiplication, and division.
Example:
1let age = 25; 2let price = 19.99;
String
The String data type is used to store text data. Strings are enclosed in single or double quotes or back tick "`".
Example:
1let name = "John"; 2let message = "Hello, World!";
Boolean
The Boolean data type has two values: true and false. It is used for making decisions in your code, such as in if statements and loops.
Example:
1let isTall = true; 2let isOpen = false;
4. Null
The Null data type represents the intentional absence of value.
Example:
1let emptyValue = null;
5. Undefined
The Undefined data type represents a variable that has been declared but not assigned a value.
Example:
1let notDefined;
Other Data Types
There are other data types on javascript like array, object, classes. We will talk about this on our future lesson.