JavaScript Data Types

Variables in JavaScript do not have any type attached to them means they are all untyped. that is why they call the language loosely type.

The loosely type means javascript does't care if we store string and later on re-assign it to a number or boolean.

In a simple way variable is like a box or container that we can use to store data inside.

Once you assign a value with some type to a variable, you can later reassign the variable to store a value of any other type, without any issue.

In JavaScript we have 2 main kinds of types:

  • Primitive Data types
  • Non-primitive Data types (Object Types)

Primitive Data types

Primitive data types are:

  • numbers
  • strings
  • booleans
  • symbols
  • null
  • undefined

Let's explain each one of them with simple example.

numbers

Number stores the data as a numerical value. Numbers are useful in counting, calculations, and comparisons. Example:


let myInteger = 1;
let cost = 1.33;

Example


let myInteger = 1;
let cost = 1.33;
console.log(myInteger);
console.log(cost);

strings

String stores character data as a string. The character data is specified by either single or double quotes. Example:


let myString = "JavaScript";
let anotherString = 'I love JavaScript';

Example


let myString = "JavaScript";
let anotherString = 'I love JavaScript';
console.log(myString);
console.log(anotherString);

booleans

Boolean stores a single bit that is either true or false. Example:


let yes = true;
let no = false;

Booleans are often used for flags.

Example


let yes = true;
let no = false;
console.log(yes);
console.log(no);

symbol

A symbol is a primitive data type introduced in ECMAScript 6 (ES6). Symbols are unique and immutable, providing a way to create unique identifiers that are guaranteed to be distinct from other values. Here's an explanation of symbols with a code example:

You can create a symbol using the Symbol()function, which returns a new, unique symbol value.


let sym1 = Symbol();
let sym2 = Symbol("descrative");
console.log(sym1.toString());// Output: Symbol()
console.log(sym2.toString());// Output: Symbol(description)

In the example above, sym1 and sym2 are two distinct symbols. The optional parameter passed to Symbol() is a description, which can be useful for debugging but does not affect the uniqueness of the symbol.

And two special types: nulland undefined.

Example


let sym1 = Symbol();
let sym2 = Symbol("descrative");
console.log(sym1.toString());// Output: Symbol()
console.log(sym2.toString());// Output: Symbol(description)

null

At times you do not have a value to store in a variable either because it hasn't been created or you are no longer using it. At this time you can set a variable to null. Example:


let newVariable = null;

Example


let newVariable = null;
console.log(newVariable);

undefined

undefined is a like null but has slide difference, there are some situation maybe you foget to assign a value to the variable, then try to access it or print it that will print undefined. Example:


let age;
let newYear = ;

Example


let age;
let newYear;
console.log(age);
console.log(newYear);

Non-Primitive Data types

Any value that's not of a primitive type (a string, a number, a boolean, null or undefined) is object.

  • array
  • onject

Array

An indexed array is a series of separate distinct data items all stored under a single variable name.

Items in the array can be accessed by their zero based index using array[index].


let fruits = ["apple","banana","orange"];
let first = fruits[0];
console.log(first);

The above code will going to print the first element in the array list which is apple.

Example


let fruits = ["apple","banana","orange"];
let first = fruits[0];
console.log(first);

another way of creating array is by using the array keyword. Let see the example.


let fruits = new Array("apple","banana","orange");
let first = fruits[0];
console.log(first);

The above also is thesame as the offer one and will give the same result apple. You can use each one of them.

Example


let fruits = new Array("apple","banana","orange");
let first = fruits[0];
console.log(first);

Object

JavaScript supports the ability to create and use object literals.

Object types have properties and also have methods that can act on those properties.

We'll talk more about objects later on in its chapter. But let's discuss some thing about it with example so that you can get insign on it.

When you use an object literal you can access values and functions in the object using the object.propertysyntax.


let person = {
  firstName:"Muhammad",
  lastName:"Ahmad"
};
let user = person.firstName;
console.log(user);

The above code will print the Muhammad as result

Example


let person = {
  firstName:"Muhammad",
  lastName:"Ahmad"
};
let user = person.firstName;
console.log(user);