Data Types in JavaScript

In this JavaScript tutorial, we are going to learn about data types in JavaScript.


What are Data Types?

  • In JavaScript, Data type defines that which type of value can be stored in a variable.

  • There are two types of Data Types in JavaScript:

    • Primitive (predefined)

    • Reference/Derived (defined by the programmer)


Primitive Types

JavaScript primitive types refer to the basic or fundamental types of data that can be used to represent simple values. There are seven primitive types in JavaScript and these are:

  • Number: Numer data type is used to represent numeric values, including integers and floating-point numbers. (for e.g. 4, 20)
const num = 35;
console.log(num);


  • String: Strings are used to represent textual data, such as a sequence of characters. (for e.g. "Raju", "Geeks Help")
const str = "Geeks Help";
console.log(str);


  • Boolean: It is used to represent a boolean value, which can be either true or false. (for e.g. true, false)
const isDeveloper = true;
console.log(isDeveloper);


  • Undefined: It represents a value that is not yet defined or initialized. (a specialized value for uninitialized variables.)
let name;
console.log(name);


  • Null: The null data type is used to represent a value that is explicitly empty or non-existent. (a unique value that symbolizes a lack of value)
const agent = null;
console.log(agent);


  • Symbol: Symbol in JavaScript are used to represent a unique and immutable value.
const firstName = Symbol('Raju');
const lastName = Symbol('WebDev');

console.log(firstName);
console.log(lastName);


  • BigInt: BigInt is a new numeric data type that can represent integers with arbitrary precision. 
const bigNum = 9007199254740991n;
console.log(bigNum);


Reference Types

Reference types in JavaScript are complex data types that are composed of a collection of values and properties. And reference types can hold more complex data than primitive types. In JavaScript, reference types are:

  • Object: JavaScript object represents a collection of key-value pairs, which can include properties and methods.

let user = {
    name: "Raju",
    lastname: "WebDev",
    isDeveloper: true,
    age: 20
}

console.log(user);
console.log(typeof user);


  • Array: Array represents an ordered collection of values, which can be of any data type.

let courses = ['HTML Simplified', 'CSS Master', 'JavaScript Doctory', 'ReactJS Ninja'];
console.log(courses);


  • Function: Functions are reusable blocks of code that can be called by other parts of the program.

function greet() {
    console.log('Hello Developer, Good Morning');
}

greet();


Note: In upcoming tutorials, we will learn about Array, Object and Functions.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Top Post Ad

Below Post Ad