Understanding JavaScript variable types
JavaScript, being a versatile and widely-used language, offers a rich set of data types that enable developers to handle and manipulate data in various forms. In this article, we will explore the different data types in JavaScript, and provide practical examples to deepen your understanding.
In JavaScript, there are several commonly used data types.Let’s explore them:
- String
- Number
- Boolean
- Undefined
- Null
- Object
String
A string represents a sequence of characters and is used to store textual data in JavaScript. There are three ways to define a string:
- Single quotes
- Double quotes
- Back-ticks
//string types
const x = 'Hello' //Single quotes
const y = "Hello World" //Double quotes
const z = `Hello World` //Backticks
//usage of string types
//double quotes
const a = "It's road"; //It's road
//single quotes with double quotes
const b = 'Who is "Jhon"?'; //Who is "Jhon"?
//double quotes with single quotes
const c = "My name is 'Jhon'"; //My name is 'Jhon'
//backtricks
const answer = `question - ${b},answer- ${c}` //question - Who is "Jhon" ,answer- My name is 'Jhon'
Number
Numerical values in JavaScript can be represented in two forms: integers and floating-point numbers. Both integer and floating-point numbers are defined in the same way in JavaScript.
//Numbers
const a = 9 //9
const b = 24.00 //24
const c = 13.54 //13.54
Boolean
Logical values in JavaScript represent “true” and “false” values. They are primarily used for conditional rendering and logical operations.
let x = 10
let y = 5
x > y //return true
x < y //return false
Undefined
In JavaScript, when a variable is declared but not assigned any value, it is represented as “undefined”. This signifies that the variable exists but currently does not hold a value.
let age; //value is undefined.
age = undefined //possible to assign a variable value as undefined.
Null
In JavaScript, the special value “null” is used to represent an empty or unknown value. It is often assigned explicitly to indicate the absence of an object or value.
const name = null;
Object
In JavaScript, the object data type is often used to store key-value pairs of data. An object can contain any data type as its values and allows for the storage of various types of data.
- Object: An object is a collection of key-value pairs, where each key is a unique identifier associated with its corresponding value.
const member = {
name : "Jhon Riky",
age : 24 ,
isEligible: true ,
}
- Array: An array is a special type of object that represents a collection of values. It contains a group of similar or related values, which can be of any data type.
const fruits = ["apple","Orange","Bananana","Papaya"];
typeof operator
Using typeof operator you can find type of a variable.
const name = "Johon";
typeof(name); //return "string"
const age = 24;
typeof(age); //return "number"
const isEligible = true;
typeof(isEligible); //return "boolean"
A solid understanding of JavaScript data types is essential for any developer looking to write clean, efficient, and reliable code.By understanding and utilizing these data types effectively, you can write more robust and dynamic JavaScript code to handle a wide range of data and scenarios.