DEV Community

Abhinav Singh
Abhinav Singh

Posted on • Updated on

JavaScript variables and Data types Bundle

Every programming language requires something to store information. Information,which is required to handle and manipulate data. JavaScript is no new to this style.

alert("Welcome to JavaScript!");
Enter fullscreen mode Exit fullscreen mode

Information is a collection of data. Information could be available to us in different forms. Some of the examples could be:

  • A Family tree : Names of the family members, age, hierarchy etc. is an information
  • Telephone Directory : Number of people in the list, their addresses and contact details are all collection of information in the directory.

JavaScript Variables

A variable can be assumed as a container for data. Suppose you have six apples. Now, you will need a box to hold them. This box is a variable in JavaScript which stores these 6 apples.
JavaScript allows it’s variables to store any kind of data. Programming languages which allow this ease of access are called “Dynamically Typed”. This means a variable can hold a number or even strings without declaring explicitly.

var box = "apple";
var name = "John Doe";
var number = 1;
Enter fullscreen mode Exit fullscreen mode

Now we know what is a variable. The question arises how our system wil understand variables.Therefore JavaScript allows us three types of variable declarations.
var keyword : We have been using var in our quite a few times in our previous examples. Variables declared in var do not lose their value ie. they have global scope. Their scope is extended within the function boundaries or they are global.

var number = 123;
Enter fullscreen mode Exit fullscreen mode

let keyword : let keyword is also used to declare variables but scope of let is limited to its block. It’s value cannot be accessed outside its block. It is also declared the same way as var. We are not going into details for now. We will discuss them in detail further.

let number = 123;
Enter fullscreen mode Exit fullscreen mode

const keyword : Values declared using const keyword cannot be changed. If trying to do so, you will get an error. Values stored under const are constant “unchangeable”.

const number = 123;
Enter fullscreen mode Exit fullscreen mode

This is all we need to know about declaring variables in JavaScript for now. We will come to a detailed study on these variable declarations in our later discussions and see how they handle data.

JavaScript Data types

Number
A number is all integers and floating point numbers. All kind of mathematical operations like addition, subtraction, multiplication and division can be performed with numbers.

let n = 123;
n = 12.35;
Enter fullscreen mode Exit fullscreen mode

String
A string is anything is JavaScript which is written in quotes. It could be either single or double quotes. JavaScript does not differentiate between them.

let name = "John Doe";
Enter fullscreen mode Exit fullscreen mode

Boolean
A boolean has two values, either “true” or “false”. It checks whether the value or condition is correct or not. We will discuss about Boolean in details later.

let isGreater= 4 > 1;
alert(isGreater);
Enter fullscreen mode Exit fullscreen mode

Null
Null represents “empty” or “no value” in JavaScript.

let age = null;
Enter fullscreen mode Exit fullscreen mode

undefined
undefined in JavaScript means “value is not defined”. Some specific functions may return undefined in JavaScript.

let x = undefined; 
Enter fullscreen mode Exit fullscreen mode

Objects
Objects are non primitive data types. Objects can be container for storing type of a data or even similar data within a single container. We do not need to go in details with objects for now.

function createUser(name,batch,marks,maxmarks){
    this.name=name;
    this.batch=batch;
    this.marks=marks;
    this.maxmarks=maxmarks;
}

createUser.prototype={
    percent:function calcPercent(){return this.marks/this.maxmarks*100;},
    grade:function calcGrade(){return this.marks>400?"A":this.marks>300?"B":this.marks>200?"C":"D";}
}
var abhinav= new createUser("abhinav",9,450,500);
Enter fullscreen mode Exit fullscreen mode

typeof operator
typeof operator returns the type of data, as simple as this and can be used for a quick check. It’s syntax is typeof(variable name).

typeof 0;//number
typeof "foo";//string
Enter fullscreen mode Exit fullscreen mode

**This was all about JavaScript variables and data types that we need to know about for now. Definitely this is not all but we will see them in action in further chapters and get a better understanding of their usage.

Till then Happy Learning!**

Top comments (4)

Collapse
 
savagepixie profile image
SavagePixie • Edited

This is a very well organised description. It looks clean and it is easy to understand.

I do have a couple of questions, though.

  1. Are there any particular things in the topic that you'd like us to discuss? I may have missed it, but I didn't see any discussion starter in the post, despite it being tagged with #discuss
  2. Did you omit symbols for any particular reason?
Collapse
 
shivam_s_chahar profile image
Shivam Chahar

Nice work!! ☺️

Collapse
 
abhinavdotdev profile image
Abhinav Singh

Thank You!

Collapse
 
dhkamp profile image
David Hölkeskamp

Very well written. I love the topic "Variables/DataTypes in JS" because there is so much one could talk about - I'm hyped for posts to come :)