Variable Types

JavaScript

There are some different types of data that can be stored in a variable.

Numbers

We can either store integers like 1, 10, 4564, -20 or decimal numbers like 4.5, -565.7656 (also called floating point number). No need to use quotes ("").

Strings

We can store text which we refer as string. We use single or double quotes to wrap the text.

Boolean

The two values true and false come under Boolean.

Arrays

An array is a single object that contains multiple values enclosed in square brackets and separated by commas.

Every position of the values in array is termed as index. The starting index is 0;

Objects

In programming, an object is a structure of code that models a real-life object. You can have a simple object that represents a box and contains information about its width, length, and height; or you could have an object that represents a person, and contains data about their name, height, weight, what language they speak, how to say hello to them, and more.

Let's us discuss another example of Objects;
let obj = { "first name": "Harry" }; // here we have space in the property "first name"
Now if you want to access "first name" of obj; then we have square bracket notation
obj["first name"]; // returns "Harry"


JavaScript is a "dynamically typed language", which means that, unlike some other languages, you don't need to specify what data type a variable will contain (numbers, strings, arrays, etc).

For Example:

let dummyString = 'Hello';
Even if the value contains numbers, it is still a string, as it is enclosed within quotes, so be careful:
let myNumber = '500'; // this is still a string type
typeof myNumber; // this is how we check for the type of a variable, with the use of typeof followed by the variable
myNumber = 500; // now this is a number
typeof myNumber;

Please do try all of the above examples by yourself !
Remember, practice makes a man perfect 😄

Loading...

Reset

  Thank you for your love and generous support  😄

DONATE