JavaScript Syntax and Basics
1. Variables and Data Types
In JavaScript, variables are used to store data values. They can be declared using the keywords var, let, or const. The var keyword is function-scoped, while let and const are block-scoped.
JavaScript supports several data types, including:
- Number: Represents both integer and floating-point numbers.
- String: Represents text data enclosed in single or double quotes.
- Boolean: Represents a logical entity and can have two values:
trueorfalse. - Undefined: Represents a variable that has been declared but not assigned a value.
- Null: Represents the intentional absence of any object value.
- Object: Represents a collection of properties, and is a non-primitive data type.
Example:
let age = 25; const name = "John"; let isStudent = true; let job; let car = null;2. Operators and Expressions
Operators in JavaScript are used to perform operations on variables and values. Common operators include:
- Arithmetic Operators: Used to perform arithmetic operations like addition (
+), subtraction (-), multiplication (*), division (/), and modulus (%). - Assignment Operators: Used to assign values to variables, such as
=,+=,-=, etc. - Comparison Operators: Used to compare values, such as
==,===,!=,!==,>,<, etc. - Logical Operators: Used to perform logical operations, such as
&&(logical AND),||(logical OR), and!(logical NOT).
Example:
let x = 10; let y = 5; let sum = x + y; let isEqual = (x === y); let isGreater = (x > y);3. Control Structures
Control structures in JavaScript are used to control the flow of execution in a program. The most common control structures are:
- Conditional Statements: Used to perform different actions based on different conditions. The
ifstatement is the most basic conditional statement. - Loops: Used to execute a block of code multiple times. Common loops include
for,while, anddo...while.
Example:
let num = 7; if (num > 5) { console.log("Number is greater than 5"); } else { console.log("Number is 5 or less"); } for (let i = 0; i < 5; i++) { console.log("Iteration: " + i); }4. Functions
Functions in JavaScript are blocks of code designed to perform a particular task. They can be defined using the function keyword and can be called multiple times within a program.
Functions can take parameters and return values. They help in organizing code and making it reusable.
Example:
function greet(name) { return "Hello, " + name + "!"; } let greeting = greet("Alice"); console.log(greeting);5. Arrays and Objects
Arrays and objects are used to store collections of data in JavaScript. An array is a special variable that can hold more than one value at a time, while an object is a collection of key-value pairs.
Arrays are zero-indexed, meaning the first element is at index 0. Objects use keys to access their values.
Example:
let fruits = ["Apple", "Banana", "Cherry"]; console.log(fruits[1]); // Outputs: Banana let person = { firstName: "John", lastName: "Doe", age: 30 }; console.log(person.firstName); // Outputs: John