DEV Community

Shoyab khan
Shoyab khan

Posted on

Day 4 of 30: JavaScript

Hey there! In the last post, we covered how comments are used and variables are declared in JavaScript. Today, we'll explore data types in JavaScript. Let's get started.

JavaScript provides two main types of data types:

  1. Primitive Data Types
  2. Non-Primitive Data Types

Note: JavaScript is a dynamically typed language, so you don't need to specify the data type of a variable. Simply declare it using var, let, or const.

Primitive Data Types

Primitive data types are basic types that are not objects and do not have methods. They are immutable, meaning their values cannot be changed. When you manipulate a primitive value, you're working directly with its value rather than with a reference. Primitive values are stored directly in memory.

The six primitive data types are:

  1. String: Represents a sequence of characters.
   let greeting = "hello";
Enter fullscreen mode Exit fullscreen mode
  1. Number: Represents both integer and floating-point numbers.
   let age = 42;
   let pi = 3.14;
Enter fullscreen mode Exit fullscreen mode
  1. Boolean: Represents a logical entity, either true or false.
   let isAvailable = true;
Enter fullscreen mode Exit fullscreen mode
  1. Null: Represents the intentional absence of any value.
   let emptyValue = null;
Enter fullscreen mode Exit fullscreen mode
  1. Undefined: Represents a variable that has been declared but not assigned a value.
   let notAssigned;
Enter fullscreen mode Exit fullscreen mode
  1. Symbol: Represents a unique identifier (introduced in ECMAScript 6).
   let uniqueID = Symbol("id");
Enter fullscreen mode Exit fullscreen mode

Non-Primitive Data Types

Non-primitive data types in JavaScript can store collections of values and have methods and properties. They are mutable, meaning their values can be changed.

The main non-primitive data types are:

  1. Object: Complex data types that can hold key-value pairs.
   const person = {
     name: "John",
     age: 30,
     hobbies: ["reading", "coding"],
     address: {
       city: "New York",
       country: "USA"
     },
     sayHello: function() {
       console.log("Hello!");
     }
   };
Enter fullscreen mode Exit fullscreen mode
  1. Array: Ordered collections of values.
   const fruits = ["apple", "banana", "orange"];
Enter fullscreen mode Exit fullscreen mode
  1. Function: Special type of object that can be invoked to perform a task or calculate a value.
   function add(a, b) {
     return a + b;
   }
Enter fullscreen mode Exit fullscreen mode
  1. Date: Represents a specific date and time.
   const currentDate = new Date();
Enter fullscreen mode Exit fullscreen mode

That’s all for this post. In the next post, we will discuss operators and conditional statements. Stay connected and don’t forget to follow me!

Top comments (1)

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

You're missing BigInt from the primitive types.