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!
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.
JavaScript has five primitive data types:
Let's explore each of them in detail.
Numbers in JavaScript are used to represent mathematical and real values.
// Simple example
let myNumber = 123;
console.log(myNumber); // Output: 123Strings are used to store text or a sequence of characters.
// Simple example
let myText = "Hello, World!";
console.log(myText); // Output: Hello, World!Booleans are used to store true/false values.
// Simple example
let isItRaining = true;
let isItSunny = false;The null value represents an intentional absence of any object value.
let myNull = null;
console.log(myNull); // Output: nullThe undefined value represents a variable that has been declared but has not been assigned a value yet.
let myUndefined;
console.log(myUndefined); // Output: undefinedUnlike primitive data types, non-primitive data types are objects in JavaScript. They are:
Let's dive into these data types.
Objects in JavaScript are used to store collections of properties and methods.
// Simple example
let myObject = {
name: "John Doe",
age: 30,
occupation: "Developer"
};
console.log(myObject); // Output: { name: 'John Doe', age: 30, occupation: 'Developer' }Arrays are used to store multiple values in a single variable.
// Simple example
let myArray = [1, "Two", true];
console.log(myArray); // Output: [1, "Two", true]Functions are reusable pieces of code that can be called as needed.
// Simple example
function greet(name) {
console.log("Hello, " + name + "!");
}
greet("World"); // Output: Hello, World!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! 🎉