Javascript
Different Datatypes in Javascript
JavaScript is a most used programming language that uses different datatypes to deal with various kinds of information.
In the world of programming, a datatype is a classification that specifies which type of value a variable can hold. It helps define the nature of the data and dictates the operations that can be performed on it. Think of datatypes as the building blocks that structure the information within your code
let num = 42;
let text = "Hello, World!";
true
or false
.let isTrue = true;
let data = null;
let undefinedVar;
let sym = Symbol("example");
let person = { name: "John", age: 30 };
let numbers = [1, 2, 3, 4, 5];
// Number: Represents any kind of number
let age = 25;
let pi = 3.14;
// String: Represents text
let greeting = "Hello, World!";
let name = 'John';
// Boolean: Represents a simple choice (true or false)
let isCodingFun = true;
let isRainyDay = false;
// Null: Represents the absence of any information
let noData = null;
// Undefined: Represents a variable that hasn't been given a value yet
let notDefined;
// Symbol: Represents a unique identifier
let uniqueKey = Symbol("example");
// Object: Represents a bunch of information organized in pairs
let person = {
name: "Alice",
age: 30,
isStudent: true
};
// Array: Represents a list of information
let numbers = [1, 2, 3, 4, 5];
let fruits = ['apple', 'orange', 'banana'];
// Using typeof to identify datatypes
console.log(typeof age); // Outputs: number
console.log(typeof greeting); // Outputs: string
console.log(typeof isCodingFun); // Outputs: boolean
console.log(typeof noData); // Outputs: object (Note: typeof null is an interesting quirk in JavaScript)
console.log(typeof notDefined); // Outputs: undefined
console.log(typeof uniqueKey); // Outputs: symbol
console.log(typeof person); // Outputs: object
console.log(typeof numbers); // Outputs: object (Arrays are also objects in JavaScript)
In JavaScript, datatypes are classified as either primitive or non-primitive. Primitive types are fundamental and immutable, while non-primitive types are more complex and mutable.
Difference between Primitive and Non-Primitive Datatypes in Javascript
Determining the type of a value in JavaScript is crucial. The typeof
operator can be used for this purpose.
For Example:
let exampleValue = "Hello, World!";
console.log(typeof exampleValue);
// Outputs: string
Beyond the core questions, let’s consider additional aspects that deepen our understanding of JavaScript datatypes:
JavaScript datatypes might seem a bit complex at first, but they’re like tools in your coding toolbox. The more you understand them, the more powerful you become as a coder.
This is just the beginning of the journey into JavaScript datatypes. Feel free to explore further and discover how these tools can make your coding adventures even more exciting!