JavaScript Data Types and Variables
Introduction
JavaScript data types and variables are the foundation of every JavaScript program. JavaScript stores information in variables, and every value in JavaScript has a specific data type. If you want to learn JavaScript programming, then understanding JavaScript data types and variables is the first and most important step.
What is a Variable in JavaScript?
A variable is a container that stores data. We use variables to store names, numbers, results, user input, and much more while working with JavaScript data types and variables.
Example:
-
var = old way
-
let = modern and recommended
-
const = cannot change
JavaScript Data Types
When we study JavaScript data types and variables, we learn that JavaScript mainly has two categories:
Primitive Data Types in JavaScript
-
String
-
Number
-
Boolean
-
Null
-
Undefined
Non-Primitive Data Types in JavaScript
-
Object
-
Array
-
Function
String Data Type
Stores text inside quotes.
Number Data Type
Stores numeric values.
Boolean Data Type
Stores true or false
Null Data Type
Represents empty value
Array Data Type
Stores multiple values in one variable
Your example:
Result is: object
Using typeof Operator with JavaScript Data Types and Variables
The typeof operator tells the data type of a value.
Examples:
Conclusion
-
JavaScript data types and variables are the base of programming
-
Variables store data
-
Data types define the kind of data
-
typeofhelps identify data types
Once you clearly understand JavaScript data types and variables, learning functions, loops, and DOM manipulation becomes much easier.
My GitHub Repository for JavaScript Code
All example codes related to JavaScript data types and variables are available in my GitHub repository:
JavaScript Codes – My GitHub Repo
You can open the files, study the code, and run them in your browser.
Read More
To understand the basics first, read my complete guide on Introduction to JavaScript.