JS Data Types 🎯

beginner
19 min

JS Data Types 🎯

Welcome to our deep dive into JavaScript (JS) Data Types! This tutorial is designed to help both beginners and intermediates understand the fundamental building blocks of JavaScript, making it easier to navigate real-world projects. Let's get started!

Understanding Data Types 📝

Data types in JavaScript define the nature and structure of the data a variable can hold. Understanding them is crucial as it helps in making more efficient and effective code.

Primitive Data Types 💡

JavaScript has five primitive data types:

  1. Number
  2. String
  3. Boolean
  4. Null
  5. Undefined

Let's explore each of them in detail.

Number 📝

Numbers in JavaScript are used to represent mathematical and real values.

javascript
// Simple example let myNumber = 123; console.log(myNumber); // Output: 123

String 📝

Strings are used to store text or a sequence of characters.

javascript
// Simple example let myText = "Hello, World!"; console.log(myText); // Output: Hello, World!

Boolean 📝

Booleans are used to store true/false values.

javascript
// Simple example let isItRaining = true; let isItSunny = false;

Null 📝

The null value represents an intentional absence of any object value.

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

Undefined 📝

The undefined value represents a variable that has been declared but has not been assigned a value yet.

javascript
let myUndefined; console.log(myUndefined); // Output: undefined

Non-Primitive Data Types 💡

Unlike primitive data types, non-primitive data types are objects in JavaScript. They are:

  1. Object
  2. Array
  3. Function

Let's dive into these data types.

Object 💡

Objects in JavaScript are used to store collections of properties and methods.

javascript
// Simple example let myObject = { name: "John Doe", age: 30, occupation: "Developer" }; console.log(myObject); // Output: { name: 'John Doe', age: 30, occupation: 'Developer' }

Array 💡

Arrays are used to store multiple values in a single variable.

javascript
// Simple example let myArray = [1, "Two", true]; console.log(myArray); // Output: [1, "Two", true]

Function 💡

Functions are reusable pieces of code that can be called as needed.

javascript
// Simple example function greet(name) { console.log("Hello, " + name + "!"); } greet("World"); // Output: Hello, World!
Quick Quiz
Question 1 of 1

Which of the following is not a primitive data type in JavaScript?

By now, you should have a good understanding of the various data types in JavaScript. Keep practicing, and soon you'll be able to write efficient and effective code like a pro! 🎉