Master JavaScript: Essential Notes for Every Developer

Creative Web Pixel

22 Jun 2024 · 4

JavaScript Data Types and Variables

 

Data Types

JavaScript provides different data types to hold different types of values. There are six primitive data types and one complex data type:

 

Number

Represents both integer and floating-point numbers.

Example: 42, 3.14

 

String

Represents a sequence of characters enclosed in single quotes (' '), double quotes (" "), or backticks (`` `).

Example: "Hello, World!", 'JavaScript'

 

Boolean

Represents a logical entity and can have only two values: true or false.

Example: true, false

 

Undefined

A variable that has been declared but has not yet been assigned a value.

Example: let x; console.log(x); // undefined

 

Null

Represents the intentional absence of any object value.

Example: let y = null;

 

Symbol

Represents a unique and immutable primitive value.

Example: const sym = Symbol('description');

 

Object

A collection of properties, where each property is a key-value pair.

const person = { firstName: "John", lastName: "Doe", age: 30 };

 

 

Variables

Variables are containers for storing data values. In JavaScript, you can declare variables using var, let, or const.

 

var

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

 

let

Block-scoped.

Can be updated but not re-declared within the same scope.

Example:

let age = 25; age = 26; // Update

 

const

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

Copyright 2024 By Creative Web Pixel