JavaScript Basics
In this section, we’ll cover the fundamental building blocks of JavaScript. Understanding these concepts will lay a solid foundation for everything you’ll learn later. This includes variables, data types, operators, and control structures, all of which are essential in creating any JavaScript program.
Variables
Variables are used to store information that can be referenced and manipulated in your code. JavaScript allows you to declare variables using three keywords: var, let, and const.
var: The original way to declare variables in JavaScript. It is function-scoped, which can lead to issues in block-scoped environments. It’s generally recommended to useletorconstinstead.let: Introduced in ES6,letallows you to declare variables that are block-scoped, meaning their values can be updated but are only accessible within the block in which they are defined.const: Also introduced in ES6,constis used to declare variables whose values cannot be changed once assigned. This is ideal for constants.
Examples:
var name = "Alice";
let age = 25;
const country = "USA";
// Updating variables
age = 26; // This works since `let` allows updates
// country = "Canada"; // This would throw an error since `const` does not allow reassignment
Data Types
JavaScript has several data types, which can be classified into two categories: primitive and object.
Primitive Data Types
- String: Textual data, enclosed in quotes.
let greeting = "Hello, World!"; - Number: Numeric values, including both integers and floating-point numbers.
let score = 95.5; - Boolean: Represents a logical entity, either
trueorfalse.let isActive = true; - Undefined: A variable that has been declared but has not been assigned a value.
let value; console.log(value); // undefined - Null: Represents the intentional absence of any value.
let data = null; - Symbol: A unique and immutable primitive value, often used for object property keys.
let symbol = Symbol("unique"); - BigInt: Allows the representation of numbers larger than what the
Numbertype can handle.let bigNumber = 1234567890123456789012345678901234567890n;
Objects
Objects are collections of key-value pairs and can represent more complex data.
Example
let person = {
name: "Alice",
age: 25,
isStudent: false,
};
Basic Operators
Operators are symbols that perform operations on variables and values. They can be classified into different types.
Arithmetic Operators
+: Addition-: Subtraction*: Multiplication/: Division%: Modulus (remainder)**: Exponentiation
Example
let sum = 5 + 3; // 8
let product = 4 * 2; // 8
let remainder = 10 % 3; // 1
Assignment Operators
=: Assigns a value+=: Adds and assigns-=: Subtracts and assigns
Example
let a = 10;
a += 5; // a = 15
a -= 3; // a = 12
Comparison Operators
These operators compare two values and return a Boolean (true or false).
==: Equal to (converts types if necessary)===: Strict equal to (no type conversion)!=: Not equal to!==: Strict not equal to>: Greater than<: Less than
Example
console.log(5 == "5"); // true (loose equality)
console.log(5 === "5"); // false (strict equality)
Logical Operators
&&: Logical AND||: Logical OR!: Logical NOT
Example
let isAdult = true;
let hasPermission = false;
console.log(isAdult && hasPermission); // false
console.log(isAdult || hasPermission); // true
console.log(!isAdult); // false
Control Structures
Control structures allow you to control the flow of the program based on conditions.
if, else, and else if Statements
Used to execute blocks of code based on certain conditions.
Example
let temperature = 30;
if (temperature > 35) {
console.log("It's too hot!");
} else if (temperature > 25) {
console.log("It's warm.");
} else {
console.log("It's cold.");
}
switch Statement
The switch statement is used when you have multiple possible values for a variable and want to execute different code for each value.
Example
let day = 3;
let dayName;
switch (day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
default:
dayName = "Invalid day";
}
console.log(dayName); // "Wednesday"
Summary
In this section, you’ve learned the basics of JavaScript, including how to declare and use variables, understand different data types, use basic operators for computations, and control the flow of your program with conditional statements. Mastering these fundamentals is crucial before moving on to more advanced topics such as functions and object-oriented programming.