JavaScript is a versatile programming language used to create interactive and dynamic web pages. It runs in the browser and can manipulate HTML, CSS, and more.
JavaScript Syntax
JavaScript syntax includes statements, semicolons, and curly braces for code blocks.
let x = 5;
if (x > 0) {
console.log("Positive number");
}
Variables & Data Types
Variables are declared using var, let, or const. Data types include string, number, boolean, object, array, null, undefined, and symbol.
let name = "Alice";
const age = 25;
var isStudent = true;
JavaScript Comments
Comments help document your code:
// This is a single-line comment
/* This is a multi-line comment */
Functions
Functions are blocks of code designed to perform a task.
function greet(name) {
return "Hello, " + name + "!";
}
// ES6 arrow function
const add = (a, b) => a + b;
Arrays
Arrays store multiple values in a single variable.
let colors = ["red", "green", "blue"];
console.log(colors[1]); // green
Objects
Objects store key-value pairs.
let person = {
name: "Bob",
age: 30,
isStudent: false
};
console.log(person.name); // Bob
Loops
Loops are used to repeat code:
for (let i = 0; i < 5; i++) {
console.log(i);
}
// while loop
let j = 0;
while (j < 5) {
j++;
}
Links & Events
JavaScript can handle events like clicks, mouseovers, etc.:
Lists in JavaScript are usually arrays, but you can also create HTML lists dynamically:
let fruits = ["Apple", "Banana", "Cherry"];
let ul = document.createElement('ul');
fruits.forEach(fruit => {
let li = document.createElement('li');
li.textContent = fruit;
ul.appendChild(li);
});
document.body.appendChild(ul);
Tables
You can create and manipulate tables with JavaScript:
let table = document.createElement('table');
for (let i = 0; i < 3; i++) {
let row = document.createElement('tr');
for (let j = 0; j < 3; j++) {
let cell = document.createElement('td');
cell.textContent = `Row ${i+1}, Col ${j+1}`;
row.appendChild(cell);
}
table.appendChild(row);
}
document.body.appendChild(table);
🎉 Certificate of Completion 🎉
This is to certify that you have successfully completed the JavaScript course.