JavaScript Part 2: Basic Data Types and Variables in JavaScript


Please Subscribe Youtube| Like Facebook | Follow Twitter

Data Types and Variables in JavaScript

JavaScript is a dynamic programming language, which means that it can use different data types without declaring them explicitly. In this article, we’ll discuss the basic data types and variables in JavaScript.

Data Types in JavaScript

JavaScript has seven basic data types, which are:

  1. Number
  2. String
  3. Boolean
  4. Null
  5. Undefined
  6. Object
  7. Symbol (added in ECMAScript 6)


The Number data type represents numeric values. It includes integers, decimals, and special numeric values such as Infinity and NaN. You can declare a Number variable as follows:

var a = 10;
var b = 3.14;
var c = Infinity;
var d = NaN;


The String data type represents a sequence of characters. You can declare a String variable as follows:

var a = "Hello";
var b = 'World';


The Boolean data type represents a logical value that can be either true or false. You can declare a Boolean variable as follows:

var a = true;
var b = false;


The Null data type represents a deliberate non-value. You can declare a Null variable as follows:

var a = null;


The Undefined data type represents a variable that has not been assigned a value. You can declare an Undefined variable as follows:

var a;


The Object data type represents a collection of properties. You can declare an Object variable as follows:

var person = {
name: "John",
age: 30,
city: "New York"


The Symbol data type represents a unique identifier. You can declare a Symbol variable as follows:

var sym1 = Symbol();
var sym2 = Symbol("foo");

Variables in JavaScript

In JavaScript, you can declare a variable using the var, let, or const keyword.


The var keyword is used to declare a variable that can be reassigned. For example:

var a = 10;
a = 20;


The let keyword is used to declare a variable that can be reassigned within its block scope. For example:

let a = 10;
if (true) {
let a = 20;


The const keyword is used to declare a variable that cannot be reassigned. For example:

const PI = 3.14;

Example Program

Here’s an example program that demonstrates the use of variables and data types in JavaScript:

// Number data type
let a = 42;
console.log("Value of a:", a);
console.log("Data type of a:", typeof a);

// String data type
let b = "Hello, World!";
console.log("Value of b:", b);
console.log("Data type of b:", typeof b);

// Boolean data type
let c = true;
console.log("Value of c:", c);
console.log("Data type of c:", typeof c);

// Null data type
let d = null;
console.log("Value of d:", d);
console.log("Data type of d:", typeof d);

// Undefined data type
let e;
console.log("Value of e:", e);
console.log("Data type of e:", typeof e);

// Object data type
let f = {name: "John", age: 30};
console.log("Value of f:", f);
console.log("Data type of f:", typeof f);

// Array data type
let g = [1, 2, 3, 4, 5];
console.log("Value of g:", g);
console.log("Data type of g:", typeof g);


Value of a: 42
Data type of a: number
Value of b: Hello, World!
Data type of b: string
Value of c: true
Data type of c: boolean
Value of d: null
Data type of d: object
Value of e: undefined
Data type of e: undefined
Value of f: { name: 'John', age: 30 }
Data type of f: object
Value of g: [ 1, 2, 3, 4, 5 ]
Data type of g: object


In this article, we discussed the basic data types in JavaScript and how to declare and use variables. By understanding these fundamental concepts, you’ll be able to write more complex programs in JavaScript.

JavaScript Beginner Tutorial Series

Please Subscribe Youtube| Like Facebook | Follow Twitter

Leave a Reply

Your email address will not be published. Required fields are marked *