JavaScript Part 2: Basic Data Types and Variables in JavaScript

JAVASCRIPT PART 2: UNDERSTANDING 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)

Number

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;

String

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

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

Boolean

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;

Null

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

var a = null;

Undefined

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

var a;

Object

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"
};

Symbol

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.

var

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

var a = 10;
a = 20;

let

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;
}

const

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);

Output:

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

Conclusion

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 *