JavaScript Guide – All You Need To Know About Variables and Datatypes

Posted by TotalDC

In previous article we discussed what is JavaScript and now lets start learning. In this article, we will get down to the real basics, looking at how to work with the most basic building blocks of JavaScript — Variables.

Datatypes in JavaScript

JavaScript is dynamically typed scripting language. That means that in JavaScript variables can receive different data types over time.

The latest ECMAScript(ES6) standard defines seven data types out of which six data types are Primitive(predefined).

  • Numbers: 5, 6.5, 7 etc.
  • String: “Hello World” etc.
  • Boolean: Has two values: true or false.
  • Null: This type has only one value : null.
  • Undefined: A variable that has not been assigned a value is undefined.
  • Object: It is the most important data-type and forms the building blocks for modern JavaScript.

JavaScript Variables

Variables in JavaScript like in other programming languages are containers which hold reusable data. It’s basic unit of storage in program.

  • The value stored in a variable can be changed during program execution.
  • A variable is only a name given to a memory location, all the operations done on the variable effects that memory location.
  • In JavaScript, all the variables must be declared before they can be used.

Before ES2015 JavaScript variables were declared using var keyword followed by the name of the variable and semi-colon. After ES2015 we now have two new variables: let and const. Let variable type shares lots of similarities with var but unlike var it has scope constrains.

let a = 3;
a = 4; // works just fine.

Const is variable type is used when you want value of that variable would never change.

const name = 'Paul';
name = 'Thom'; // will give an error.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: