js_learning
js_learning
JavaScript is a powerful programming language used to make web pages interactive. It runs in the
browser and can be used for tasks such as handling user input, creating animations, and modifying
webpage content dynamically.
1. JavaScript Basics
Variables
Variables store data that can be used later. In JavaScript, you can declare variables using let,
const, or var.
let name = "John"; // A string variable
const age = 25; // A constant number variable
var city = "New York"; // A variable declared with var (older syntax)
Data Types
JavaScript supports different types of data:
String: Text ("Hello, World!")
Number: Numeric values (42, 3.14)
Boolean: True or false
Array: A collection of values ([1, 2, 3])
Object: Key-value pairs ({name: "Alice", age: 30})
Operators
JavaScript includes arithmetic, comparison, and logical operators:
let sum = 5 + 3; // Addition
let isEqual = (10 === 10); // Comparison
let isTrue = (true && false); // Logical AND
2. Functions
Functions allow you to write reusable blocks of code.
function greet(name) {
return "Hello, " + name + "!";
}
console.log(greet("Alice"));
3. Conditional Statements
Use if, else if, and else to control the flow of your code.
let score = 85;
if (score >= 90) {
console.log("Grade: A");
} else if (score >= 80) {
console.log("Grade: B");
} else {
console.log("Grade: C");
}
4. Loops
Loops allow you to execute code multiple times.
for (let i = 0; i < 5; i++) {
console.log("Iteration: " + i);
}
5. DOM Manipulation
JavaScript can modify HTML elements dynamically.
document.getElementById("demo").innerHTML = "Hello, JavaScript!";
Conclusion
JavaScript is an essential language for web development. Keep practicing, and try writing small
scripts to strengthen your understanding!