Javascript
What is JavaScript
JavaScript is one of the most popular and powerful programming languages used in modern web development. It allows developers to create interactive, dynamic, and user-friendly websites. While HTML provides structure and CSS provides styling, JavaScript brings life to a website.
With JavaScript, you can:
- Create interactive forms
- Build dynamic web pages
- Develop web applications
- Handle events like clicks, scrolls, and inputs
- Connect with servers and databases
- Build full-stack applications using frameworks
JavaScript runs directly in the browser, making it essential for frontend development and also widely used in backend development with Node.js.
How JavaScript Works
JavaScript runs inside the browser using a JavaScript engine (like Chrome’s V8 engine). It executes code line by line and responds to user actions.
Adding JavaScript to a Web Page
You can include JavaScript in three ways:
- Inline JavaScript
- Internal JavaScript
- External JavaScript file
Example:
<script> alert("Welcome to JavaScript Course!");
</script>Variables and Data Types
Variables
Variables are used to store data values.
let name = "John";
const age = 25;
var city = "London";
Types of Variables
let→ block-scoped variableconst→ constant value (cannot be changed)var→ older way (not recommended)
Data Types in JavaScript
- String → “Hello”
- Number → 10, 20.5
- Boolean → true/false
- Array → [1, 2, 3]
- Object → {name: “John”}
- Undefined
- Null
Operators
JavaScript includes different types of operators:
Arithmetic Operators
let a = 10;
let b = 5;
console.log(a + b); // 15
console.log(a - b); // 5
console.log(a * b); // 50
console.log(a / b); // 2- == (equal to)
- === (strict equal)
- != (not equal)
, <, >=, <=
🔹 Logical Operators
- && (AND)
- || (OR)
- ! (NOT)
Loops in JavaScript
Loops are used to execute a block of code repeatedly.
For loop
for(let i = 1; i <= 5; i++){
console.log(i);
}let i = 1;
while(i <= 5){
console.log(i);
i++;
}
Executes at least once.