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 name = "Ali";
let age = 20;
const country = "Pakistan";
  • 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.

var name = "Aptech";

Number Data Type

Stores numeric values.

var marks = 95;
document.write(12343232);

Boolean Data Type

Stores true or false

var isPassed = true;

Null Data Type

Represents empty value

var data = null;

Array Data Type

Stores multiple values in one variable

["Ali", "Raza", "Ahmed"]

Your example:

document.write(typeof(["Ali" , 'Raza' , 'Ahmed']));

Result is: object


Using typeof Operator with JavaScript Data Types and Variables

The typeof operator tells the data type of a value.

Examples:

typeof("Connect Nisa") // string

typeof(7894564) // number

typeof(true) //boolean

typeof(null) //object

typeof(["Ali","Raza"]) // object


Conclusion

  • JavaScript data types and variables are the base of programming

  • Variables store data

  • Data types define the kind of data

  • typeof helps 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts

Categories: