Contents

Variables and Data Types

JavaScript, like most programming languages, uses variables to store data values and manipulate information throughout a program. Understanding how to use variables and data types effectively is crucial for building functional and dynamic web applications.

Variables

A variable is a container for storing data values. It allows you to label and store data that you can use or change later in your script. In JavaScript, variables can be declared using var, let, or const.

1.1 Declaring Variables
  • let: Introduced in ES6, let allows you to declare block-scoped variables. This means the variable is only accessible within the block (enclosed by {}) in which it was declared.

				
					let name = "Alice";
let age = 30;

				
			
  • const: Also introduced in ES6, const is used to declare variables that are constant and cannot be reassigned after their initial assignment. const is block-scoped, similar to let.
				
					const pi = 3.14;

				
			
  • var: The traditional way of declaring variables in JavaScript, which is function-scoped. It is generally recommended to use let and const over var for better code predictability and scope management.
				
					var city = "New York";


				
			
1.2 Naming Variables
  • Variable names are case-sensitive (name and Name are different).

  • Variable names must start with a letter, underscore (_), or dollar sign ($). They cannot start with a number.

  • Use descriptive names that indicate the variable’s purpose.

				
					let userAge = 25; // Descriptive name
let _isAdmin = true;



				
			
1.3 Initializing Variables
  • You can declare a variable without initializing it. Uninitialized variables have a default value of undefined.

				
					let score;
console.log(score); // Output: undefined

score = 100; // Now the variable is initialized


				
			

Data Types

Data types specify the kinds of values that can be stored in variables. JavaScript is a dynamically typed language, which means that variables can change types as the script executes.

2.1 Primitive Data Types

JavaScript has several primitive data types, which are the most basic kinds of data.

  • String: Represents a sequence of characters enclosed in single ('...'), double ("..."), or backticks (`...` for template literals).

				
					let greeting = "Hello, World!";
let name = 'John';
let message = `Hello, ${name}`; // Template literals allow embedding variables

				
			
  • Number: Represents both integers and floating-point numbers.
				
					let age = 30;
let temperature = 98.6;

				
			
  • Boolean: Represents a logical entity and can have only two values: true or false.
				
					let isOnline = true;
let isAvailable = false;

				
			
  • Undefined: A variable that has been declared but not initialized has the value undefined.
				
					let score;
console.log(score); // Output: undefined

				
			
  • Null: Represents the intentional absence of any object value. It is different from undefined.
				
					let data = null;

				
			
  • Symbol: Introduced in ES6, a Symbol is a unique and immutable primitive value often used to identify object properties.
				
					let uniqueId = Symbol('id');

				
			
  • BigInt: Introduced in ES2020, BigInt allows you to represent integers larger than the Number data type can handle.
				
					let bigNumber = 123456789012345678901234567890n;

				
			
2.2 Non-Primitive (Reference) Data Types

JavaScript also has non-primitive data types, such as objects and arrays, which are used to store collections of data.

  • Object: A collection of key-value pairs. Keys are strings (or Symbols), and values can be any data type.

				
					let person = {
  name: "Alice",
  age: 25,
  isStudent: true
};

				
			
  • Array: An ordered list of values (elements). Arrays are objects but are used to store multiple values in a single variable.
				
					let colors = ["red", "green", "blue"];

				
			
  • Function: Functions are objects in JavaScript. They can be stored in variables, passed as arguments, and returned from other functions.
				
					let greet = function(name) {
  return `Hello, ${name}!`;
};

				
			

Constants

Constants are variables declared using const whose values cannot be reassigned once set. They are useful when you want to define variables that should remain constant throughout your code.

3.1 Declaring Constants
  • A constant must be initialized at the time of declaration.

  • Constants are block-scoped, similar to variables declared with let.

				
					const gravity = 9.8; // Declared and initialized

				
			
3.2 Mutability of Constant Objects and Arrays

While the reference to a constant object or array cannot be changed, the properties of an object or the elements of an array can still be modified.

				
					const person = {
  name: "John",
  age: 30
};

person.age = 31; // This is allowed because the object itself is not being reassigned

const colors = ["red", "green"];
colors.push("blue"); // Allowed; modifying the array content

				
			
3.3 When to Use const vs. let
  • Use const when you want to define a variable that will not change.
  • Use let when the variable’s value will change or when you need block-scoping.
  • Avoid using var due to its function-scoped behavior, which can lead to potential bugs.

Summary

JavaScript provides a flexible way to declare variables using let, const, and var. Variables store data types, including primitive types like strings, numbers, and booleans, and reference types like objects and arrays. Constants (const) are variables that cannot be reassigned after their initial assignment, making them ideal for values that should remain constant throughout the program. Understanding variables and data types is crucial for managing data and building dynamic, interactive JavaScript applications.