Here’s a quick beginner-friendly introduction to JavaScript!
1. Basics of JavaScript Syntax
Variables: Used to store data values. In JavaScript, you can declare variables using var,
let, or const.
javascript
Copy code
let name = "Alice";
const age = 25;
var city = "New York";
Data Types: Common data types include:
o String: Text, enclosed in quotes ("hello" or 'world').
o Number: Any numerical value, including decimals (10, 3.14).
o Boolean: Represents true or false.
o Array: A list of items ([1, 2, 3]).
o Object: A collection of key-value pairs ({name: "Alice", age: 25}).
2. Basic Operators
Arithmetic Operators: + (add), - (subtract), * (multiply), / (divide).
javascript
Copy code
let a = 5;
let b = 2;
console.log(a + b); // Output: 7
Comparison Operators: ==, ===, !=, !==, >, <, >=, <=.
javascript
Copy code
console.log(10 === 10); // Output: true
console.log(10 == "10"); // Output: true
console.log(10 === "10"); // Output: false
Logical Operators: && (and), || (or), ! (not).
javascript
Copy code
console.log(true && false); // Output: false
console.log(true || false); // Output: true
3. Control Structures
if...else: Use this to run code only if a specific condition is true.
javascript
Copy code
let age = 18;
if (age >= 18) {
console.log("You are an adult.");
} else {
console.log("You are a minor.");
}
Loops: Used to repeat code.
o for loop:
javascript
Copy code
for (let i = 0; i < 5; i++) {
console.log(i);
}
o while loop:
javascript
Copy code
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
4. Functions
Defining Functions: Functions allow you to reuse blocks of code.
javascript
Copy code
function greet(name) {
return "Hello, " + name;
}
console.log(greet("Alice")); // Output: Hello, Alice
5. DOM Manipulation (for web pages)
JavaScript can interact with HTML elements, allowing you to modify the webpage
dynamically.
javascript
Copy code
document.getElementById("myButton").onclick = function() {
alert("Button clicked!");
};
6. Practice Resources
Sites like JavaScript.info, freeCodeCamp, and Codecademy have excellent interactive
exercises for JavaScript beginners.