JS Data Types
Ø A data type specifies the amount of memory that allocates to a value associated with it. A data type also determines the operations that you can perform on it.
Ø Data types in JavaScript describe the different types or kinds of data that you will be working with and storing in variables.
Ø JavaScript is a dynamic and loosely typed language. It means you don't require to specify a type of variable.
Ø A variable in JavaScript can be assigned any type of value, as shown in the following example.
var myvariable = 0; // numeric value
myvariable = 'ten'; // string value
myvariable = 10.0; // decimal value
myvariable = true; // Boolean value
myvariable = null; // null value
In general, there are eight basic data types in JavaScript. Seven of them are primitive types: number, string, Boolean, null, undefined, symbol, and BigInt, and the other one is a complex type: object.
Ø String: represents textual data.
Example “Hello World!” ‘Hello’
Ø Number: an integer or a floating-point number.
Example: 3, 3.234, 3e-2, etc.
Ø BigInt: an integer with arbitrary precision.
Example: 900719925124740999n, 1n, etc.
Ø Boolean: Any of two values: true or false.
Example: true and false
Ø Undefined: a data type whose variable is not initialized.
Example: let a;
Ø Null: denotes a null value.
Example: let a = null;
Ø Symbol: data type whose instances are unique and immutable.
Example: let value = Symbol('hello');
Ø Object: key-value pairs of collection of data.
Example let student = { };
1) String
Ø The string is used to store text.
Ø In JavaScript, strings are surrounded by quotes.
Ø This type of data is extremely common in JavaScript.
Ø Strings are written in single or double quotes.
Ø Single quotes and double quotes are practically the same and you can use either of them. For example:
console.log(""); // empty string
console.log("Hello"); // one word
console.log("Hello, world"); // a phrase
console.log('a'); // single character
console.log('1234'); // a sequence of digits
Ø Note: any digits in quotes will also be considered a string.
2) Number
Ø The number represents integer and floating numbers (decimals and exponentials). Numbers are the most important thing for any programmer.
Ø You can use positive, negative numbers, and zeros. There are no additional difficulties in recording floating-point numbers.
Ø Integer numbers can be used to count physical objects, while floating-point numbers are a good choice for statistical and scientific calculations. For example:
console.log(100);
console.log(0);
console.log(-100);
console.log(-100.04);
3) BigInt type
Ø The BigInt type represents the whole numbers that are larger than 253 – 1. However, if you need to use a larger number than that, you can use the BigInt data type.
Ø A BigInt number is created by appending n to the end of an integer. For example,
let value1 = 900719925124740998n;
4) Boolean
Ø This data type represents logical entities. Boolean represents one of two values: true or false. For example:
let a = true;
let b = false;
console.log(typeof b); // Boolean
Ø In general, JavaScript allows values of other types to be converted into Boolean values of true or false.
Ø To convert a value of another data type into a Boolean value, you use the Boolean () function.
5) undefined
Ø The undefined data type represents the value that is not assigned.
Ø If a variable is declared but the value is not assigned, then the value of that variable will be undefined. For example:
let name;
console.log(name); // shows "undefined"
6) null
Ø In JavaScript, null is a special value that represents an empty or unknown value. For example:
let name= null;
console.log(name); // shows "null"
Ø Note: null is not a “reference to a non-existing object” or a “null pointer” like in some other languages.
7) Symbol
Ø A Symbol is a unique and immutable primitive value.
Ø This data type was introduced in a newer version of JavaScript.
Ø The purpose of symbols is to create unique property keys that are guaranteed not to clash with keys from other codes.
Ø You use the Symbol () function to create a Symbol.
Ø A value having the data type Symbol can be referred to as a symbol value.
Ø The symbol is an immutable primitive value that is unique. For example:
let name1 = Symbol('John');
let name2 = Symbol('John');
Ø name1 and name2 both contain John, they are different as they are of the symbol type.
8) Objects
Ø An object is a complex data type that allows us to store collections of data.
Ø An object holds multiple values in terms of properties and methods.
Ø In JavaScript, an object is a collection of properties, where each property is defined as a key-value pair. For Example:
var person = {
Fname: 'John',
Lname: null,
age: 20
};
Ø If want to access an object’s property, you can use; the dot notation (.) and the array-like notation ([]). For Example:
console.log(person.Fname);
console.log(person['Lname']);
Leave a comment
No Cmomments yet