22 Jun 2024 · 4
JavaScript provides different data types to hold different types of values. There are six primitive data types and one complex data type:
Represents both integer and floating-point numbers.
Example: 42
, 3.14
Represents a sequence of characters enclosed in single quotes (' '
), double quotes (" "
), or backticks (`` `).
Example: "Hello, World!"
, 'JavaScript'
Represents a logical entity and can have only two values: true
or false
.
Example: true
, false
A variable that has been declared but has not yet been assigned a value.
Example: let x; console.log(x); // undefined
Represents the intentional absence of any object value.
Example: let y = null;
Represents a unique and immutable primitive value.
Example: const sym = Symbol('description');
A collection of properties, where each property is a key-value pair.
const person = { firstName: "John", lastName: "Doe", age: 30 };
Variables are containers for storing data values. In JavaScript, you can declare variables using var
, let
, or const
.
Function-scoped or globally-scoped if declared outside a function.
Can be re-declared and updated.
Example:
var name = "Alice"; var name = "Bob"; // Re-declaration name = "Charlie"; // Update
Block-scoped.
Can be updated but not re-declared within the same scope.
Example:
let age = 25; age = 26; // Update
Block-scoped.
Cannot be updated or re-declared.
Must be initialized at the time of declaration.
Example:
const birthYear = 1990;
Example Usage
// Number
let num = 10; //
String
let greeting = "Hello, JavaScript!"; //
Boolean
let isJavaScriptFun = true; // Undefined let undefinedVar; //
Null
let emptyValue = null; // Symbol const uniqueID = Symbol('id'); //
Object
const car = { make: "Toyota", model: "Camry", year: 2020 }; //
var declaration
var userName = "John"; userName = "Jane"; //
let declaration
let userAge = 30; userAge = 31;
// const declaration
const userCity = "New York";
Understanding these basic concepts of data types and variables is essential for writing effective and efficient JavaScript code.
Share on Facebook Share on Twitter
Tags: JavaScript, JS notes, JavaScript tutorials, CreativeWebpixel
Dive deeper into web development with our collection of informative blogs.
Copyright 2024 By Creative Web Pixel