All You Need To Know – JavaScript Variables And Datatypes
Posted by TotalDCIn the previous article we discussed what is JavaScript and now let’s start learning. In this article, we will get down to the real basics and learn about JavaScript variables and datatypes.
In this article you will learn:
JavaScript Datatypes
JavaScript is a 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 that hold reusable data. It’s the basic unit of storage in the 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 affect 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 constraints.
let a = 3;
a = 4; // works just fine.
Const is a variable type used when you want the value of that variable would never change.
const name = 'Paul';
name = 'Thom'; // will give an error.