JavaScript Data Types

A JavaScript variable can hold any type of data in it but still in all JavaScript has 8 Datatypes which are as follows.

1. String
2. Number
3. Bigint
4. Boolean
5. Undefined
6. Null
7. Symbol
8. Object (object can further contain ‘An Array’ & ‘A Date ‘)

// Examples of Numbers:
let length = 11;
let weight = 2.5;

//Examples of Strings:
let color = "Yellow";
let lastName = "Johnson";

//Examples of Booleans
let x = true;
let y = false;

// Examples of Object:
const person = {firstName:"John", lastName:"Doe"};

// Examples of Array object:
const cars = ["Saab", "Volvo", "BMW"];

// Examples of Date object:
const date = new Date("2023-04-04");

In programming, data types are an important concept. To be able to operate on variables, it is important to know something about types. Without the data type, the computer cannot safely solve the problem.

let x = "10" + "CodeConfig";          //Output= 10CodeConfig   
let x = 10 + 4 + "CodeConfig";       //Output= 14CodeConfig

Will this above code generate an error, or will it generate results? Conclusion is- when adding a number and a string, JavaScript will treat this number also as a string and will perform concatenation of whole data.

JavaScript Types are Dynamic

JavaScript also support dynamic types. What does this mean -it means that the same variable can be used to hold different data types of values. Let’s see below example.

let z;             // Now z is undefined
z = 2;             // Now z is a Number
z = "CodeConfig";  // Now z is a String

JavaScript Strings

Java Script is a series of characters and can be stored in variable. String can be written with double quotes and also with single quotes.

// Using double quotes:
let name1= "codeConfg";

// Using single quotes:
let name2 = 'Learn .Net Tips';

JavaScript Numbers

All JavaScript numbers are always stored as decimal numbers (floating point). Numbers can be written with, or without decimals. like

// With decimals:
let Price1 = 25.00;

// Without decimals:
let Price2 = 25;

JavaScript BigInt

All JavaScript numbers are stored in 64-bit floating-point format. JavaScript BigInt is a new data type (ES2020 onwards) that can be used to store integer values ​​that are too large to be represented by regular JavaScript numbers.

let num = BigInt("123456789012345678901234567890");

Exponential Notation

Extra large or extra small numbers can be written with scientific (exponential) notation:

let x = 123e5;      //Output= 12300000
let y = 123e-5;     //Output= 0.00123

JavaScript Booleans

Java Script Booleans always have two values: true or false and this depends upon the condtion & values passed to our condition.

let x = 1;
let y = 1;
let z = 2;
(x == y)       // Returns true in output
(x == z)       // Returns false in output

Undefined

In JavaScript, if we declare a variable without a value, then it has the value undefined. The type will also be undefined.

let student;    // Value is undefined, type is also undefined

Empty Values

Empty value is totally different than undefined. Empty string has its own value and Type too.

let Student = "";    // The value is "", using typeof its DATA TYPE is "string"

null in JavaScript

A null means no value. You assign a value of 0 to a variable with the intention that the variable has no value now but will have a value later. It’s like a placeholder for a value. The null type is object.

let Student= null;

console.log(Student);              // null
console.log(typeof Student);       // "object"

JavaScript Arrays

In JavaScript arrays items are separated by commas all values are written with square brackets. Let’s check the example below

const tech = [".NET", "JAVA", "Angular"];

JavaScript Objects

Java Script object properties are written as name: value pairs, separated by commas. This whole content is then placed/written with curly braces {}.

const student = {  firstName:"John",
                   lastName:"Smith",
                   age:15,
                   Stream:"ARTS"
                 };

Leave a Reply

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