For larger scripts, link an external .js
file:
This concludes my JavaScript learning journey from beginner to intermediate levels! I hope this guide proves helpful. Feel free to share your own learning tips or ask questions in the comments! Happy coding! ✨
","image":"http://www.luping.net/uploads/20250324/174281403267e13b504836d.jpg174281403267e13b5048375.jpg","datePublished":"2025-03-25T02:00:42+08:00","dateModified":"2025-03-25T02:00:42+08:00","author":{"@type":"Person","name":"luping.net","url":"https://www.luping.net/articlelist/0_1.html"}}This guide charts a course from JavaScript fundamentals to intermediate concepts, drawing from my personal learning experience. I've compiled key takeaways, practical examples, and helpful hints to make your learning journey smoother. Let's dive in!
Table of Contents
1. Variables
Variables are containers for data used in your programs. JavaScript offers two main ways to declare them:
let
: For variables whose values might change.const
: For values that should remain constant.Example:
let age = 25;
const name = "Mario";
Variables can hold numbers, text (strings), true/false values (booleans), or even undefined values. JavaScript provides standard arithmetic operators ( , -, *, /, %) and the exponentiation operator (**).
console.log(2 ** 3); // Output: 8
2. Arrays
Arrays store multiple values within a single variable. Use square brackets to define an array:
let fruits = ["apple", "banana", "cherry"];
Access elements using their index (starting from 0):
console.log(fruits[0]); // Output: apple
Adding and Removing Elements:
Arrays are dynamic; you can modify them:
.push()
: Adds an element to the end..unshift()
: Adds an element to the beginning..pop()
: Removes the last element..shift()
: Removes the first element.Example:
fruits.push("orange");
console.log(fruits); // Output: ["apple", "banana", "cherry", "orange"]
Searching Arrays:
.indexOf()
: Finds the index of a value..includes()
: Checks if a value exists.console.log(fruits.indexOf("banana")); // Output: 1
console.log(fruits.includes("grape")); // Output: false
3. Conditional Statements
Conditional statements allow your code to make decisions. if
and else
are commonly used:
if (grade > 60) {
console.log("You passed!");
} else {
console.log("You failed!");
}
Comparison Operators:
These operators are essential for evaluating conditions:
===
(strict equality)!==
(strict inequality)>
(greater than) (less than)
>=
(greater than or equal to) (less than or equal to)
4. Functions
Functions are reusable blocks of code. Define them using the function
keyword:
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("Alice")); // Output: Hello, Alice!
Parameters and Arguments:
Functions can accept inputs (parameters) and use them when called with arguments:
function add(a, b) {
return a b;
}
console.log(add(2, 3)); // Output: 5
5. Objects
Objects are collections of key-value pairs, like mini-databases:
const car = {
brand: "Tesla",
model: "Model 3",
year: 2020
};
console.log(car.brand); // Output: Tesla
Methods in Objects:
Objects can also contain functions (methods):
const phone = {
brand: "Apple",
ring() {
console.log("Ring, ring! ?");
}
};
phone.ring();
6. The DOM (Document Object Model)
The DOM lets JavaScript interact with HTML elements.
Selecting Elements:
Use the document
object to select elements:
const heading = document.querySelector("h1");
console.log(heading.innerText); // Logs the text within the tag
Updating Elements:
Modify content dynamically:
heading.innerText = "Welcome to JavaScript!";
7. Events
Respond to user actions (clicks, key presses) using .addEventListener()
:
button.addEventListener("click", () => {
console.log("Button clicked!");
});
Example: Incrementing a counter:
let count = 0;
button.addEventListener("click", () => {
count ;
console.log(`Clicked ${count} times`);
});
8. Integrating HTML and JavaScript
Add JavaScript directly to HTML using tags:
For larger scripts, link an external .js
file:
This concludes my JavaScript learning journey from beginner to intermediate levels! I hope this guide proves helpful. Feel free to share your own learning tips or ask questions in the comments! Happy coding! ✨
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3