Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Data Types in JavaScript


JavaScript has different data types used to represent and manipulate various kinds of values. Understanding data types is essential for effective coding. The main data types in JavaScript are string, number, boolean, null, undefined, and object. This article explains each data type with examples.

1. String

A string represents text and is created by enclosing characters within single quotes, double quotes, or backticks.

Example: Declaring and using strings

      let greeting = "Hello, World!";
      console.log(greeting);  // Output: Hello, World!

      let name = 'Alice';
      console.log("Hello, " + name);  // Output: Hello, Alice

      let age = 25;
      console.log(`Age is ${age}`);  // Output: Age is 25
    

Strings are commonly used for displaying and manipulating text in applications. JavaScript provides many methods for working with strings, such as length, toUpperCase(), and concat().

2. Number

The number data type represents both integer and floating-point numbers. JavaScript numbers are capable of representing large integers and decimals.

Example: Declaring and using numbers

      let count = 10;
      console.log(count);  // Output: 10

      let price = 19.99;
      console.log(price);  // Output: 19.99

      let total = count * price;
      console.log("Total:", total);  // Output: Total: 199.9
    

Numbers in JavaScript can be used in mathematical operations like addition, subtraction, multiplication, and division.

3. Boolean

A boolean represents a logical entity and can have only two values: true or false. Booleans are commonly used in conditional statements to control the flow of the program.

Example: Using booleans in conditional statements

      let isLoggedIn = true;
      console.log(isLoggedIn);  // Output: true

      let age = 20;
      let isAdult = age >= 18;
      console.log("Is adult:", isAdult);  // Output: Is adult: true
    

Boolean values are often the result of comparison operations like ===, >, and <.

4. Null

null represents the intentional absence of any object value. It is often used to indicate that a variable has been explicitly set to have no value.

Example: Using null

      let emptyValue = null;
      console.log(emptyValue);  // Output: null

      let user = { name: "Alice", age: 25 };
      user = null;  // User object is now null
      console.log(user);  // Output: null
    

Null is often used to indicate "no value" or "empty" state for variables that may later hold objects or other data.

5. Undefined

undefined represents a variable that has been declared but not yet assigned a value. By default, JavaScript initializes variables to undefined if they are not assigned a value.

Example: Using undefined

      let unassigned;
      console.log(unassigned);  // Output: undefined

      function sayHello(name) {
        console.log("Hello " + name);
      }
      sayHello();  // Output: Hello undefined
    

undefined is a common result when accessing non-existing properties or variables that haven’t been initialized.

6. Object

An object is a complex data type that allows storing collections of data in key-value pairs. Objects are useful for representing structured data and can contain other data types, including arrays, functions, and other objects.

Example: Creating and using an object

      let person = {
        name: "Alice",
        age: 25,
        isStudent: true
      };
      console.log(person.name);  // Output: Alice
      console.log(person["age"]);  // Output: 25

      person.city = "New York";  // Adding a new property
      console.log(person);  // Output: { name: "Alice", age: 25, isStudent: true, city: "New York" }
    

Objects allow easy grouping and access of related data, making them useful for representing entities like users, products, or configurations.

Summary of JavaScript Data Types

Conclusion

JavaScript offers several data types to store and manipulate various kinds of values, from simple text and numbers to more complex structures like objects. Understanding data types is fundamental to working effectively in JavaScript and handling data efficiently in programs.





Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java