Basic JavaScript: A Quick Start Guide
Welcome to the exciting world of JavaScript! As a versatile programming language, JavaScript is essential for creating dynamic and interactive websites. Let’s dive into the basics with this quick start guide.
1. Get started:
a. Add JavaScript to HTML:
To use JavaScript in your web pages, add the following script tag to the <head> or <body> section of your HTML document:
<html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Your Web Page</title> <script src="your-script.js"></script> </head> <body> <!-- Your HTML content here --> </body> </html>
2. Variables and data types:
a. Declaration of variables:
Use let, const, and var to declare variables.
let greeting = "Hello, World!"; const pi = 3.14; var age = 25;
b. Data Sources:
JavaScript contains a variety of data types, including strings, numbers, booleans, arrays, and objects.
let name = "John"; let number = 42; let isTrue = true; let fruits = ['apple', 'orange', 'banana']; let person = { name: 'Alice', age: 30 };
3. Functions:
a. Declaring Functions:
Create reusable blocks of code using functions.
console.log(result);
4. Control Flow:
a. Conditional Statements:
Make decisions in your code with if
, else if
, and else
statements.
let temperature = 25; if (temperature > 30) { console.log("It's hot outside!"); } else if (temperature > 20) { console.log("It's a pleasant day."); } else {
b. Loops:
Use loops like for
and while
for repetitive tasks.
for (let i = 0; i < 5; i++) { console.log("Count: " + i); } let counter = 0; while (counter < 3) { console.log("Loop iteration: " + counter); counter++; }