JavaScript Basics
JavaScript Basics
In this guide, we’ll cover fundamental concepts of JavaScript, including variables, data types, operators, control flow, and functions.
Variables
Variables are used to store data values. In JavaScript, you can declare variables using var
, let
, or const
.
1
2
3
4
// Variable declaration
var name = "John";
let age = 30;
const PI = 3.14;
Data Types
JavaScript has several primitive data types:
- Number: Represents numeric values.
- String: Represents textual data.
- Boolean: Represents true/false values.
- Undefined: Represents the absence of a value.
- Null: Represents the intentional absence of any value.
- Symbol (ES6): Represents a unique identifier.
1
2
3
4
5
6
let num = 10;
let str = "Hello";
let bool = true;
let undef;
let n = null;
let sym = Symbol("foo");
Operators
JavaScript supports various operators:
- Arithmetic Operators: +, -, *, /, %.
- Comparison Operators: ==, ===, !=, !==, <, >, <=, >=.
Logical Operators: && (AND), (OR), ! (NOT). - Assignment Operators: =, +=, -=, *=, /=.
- Ternary Operator: condition ? expr1 : expr2.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
let x = 10;
let y = 5;
// Arithmetic Operators
let sum = x + y;
let diff = x - y;
let product = x * y;
let quotient = x / y;
let remainder = x % y;
// Comparison Operators
let isEqual = x === y;
let isGreater = x > y;
// Logical Operators
let isBothTrue = (x > 0) && (y < 10);
let isEitherTrue = (x > 0) || (y > 10);
// Ternary Operator
let result = (x > y) ? "x is greater" : "y is greater";
Control Flow
Control flow statements determine the order in which statements are executed.
- Conditional Statements: if, else if, else.
- Switch Statement: Executes a block of code depending on different cases.
- Loops: for, while, do…while.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
let num = 10;
// Conditional Statements
if (num > 0) {
console.log("Positive");
} else if (num < 0) {
console.log("Negative");
} else {
console.log("Zero");
}
// Switch Statement
let day = "Monday";
switch (day) {
case "Monday":
console.log("Start of the week");
break;
case "Friday":
console.log("End of the week");
break;
default:
console.log("Midweek");
}
// Loops
for (let i = 0; i < 5; i++) {
console.log(i);
}
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
Functions
Functions are blocks of reusable code that perform a specific task.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Function Declaration
function greet(name) {
return "Hello, " + name + "!";
}
// Function Expression
let square = function(x) {
return x * x;
};
// Arrow Function (ES6)
let cube = (x) => {
return x * x * x;
};
console.log(greet("John"));
console.log(square(4));
console.log(cube(3));
These are the fundamental concepts of JavaScript that every developer should understand to start building applications.
This post is licensed under CC BY 4.0 by the author.