逐步使用代码示例来帮助您更好地理解每个概念。
javascript 是一种多功能语言,可以在浏览器或服务器上运行(使用 node.js)。它用于使网页具有交互性。
<h2>hello, javascript!</h2> <button onclick="myfunction()">click me</button> <script> function myfunction() { alert("hello, welcome to javascript!"); } </script>
javascript 可以放置在三个位置:
点我
立即学习“Java免费学习笔记(深入)”;
2. **internal** (inside a `<script>` tag): ```html <script> function showmessage() { alert("internal js"); } </script>
console.log("这是外部js!");
--- ### **3. js output** different ways to display output in javascript: ```html <!-- html output --> <p id="output"></p> <script> // write into html element document.getelementbyid("output").innerhtml = "hello world!"; // write into browser's console console.log("console output"); // display in an alert box alert("alert box"); </script>
声明是一条指令。例如:
let a = 5; // this is a statement let b = 6; // this is another statement let sum = a + b; // statement calculating the sum
语法是指 javascript 程序如何构建的一组规则:
let name = "john"; // variable declaration and assignment console.log(name); // statement to output the value of 'name'
// this is a single-line comment /* this is a multi-line comment */ let x = 10; // this is an inline comment
变量存储数据值。您可以使用 let、var 或 const 来声明它们。
let name = "alice"; // declaring a string variable var age = 30; // declaring a number variable const country = "usa"; // declaring a constant
let 关键字允许您声明具有块作用域的变量。
let x = 10; if (x > 5) { let y = 20; // 'y' only exists inside this block } console.log(y); // error: y is not defined
用 const 声明的变量是不可变的(不能重新分配它们)。
const pi = 3.14159; pi = 3.14; // error: assignment to constant variable.
运算符用于对变量和值执行运算:
let a = 10; let b = 20; let sum = a + b; // arithmetic operator let isequal = (a == b); // comparison operator let notequal = (a != b); // logical operator
javascript 提供了基本的算术运算符:
let x = 5; let y = 2; let sum = x + y; // addition let difference = x - y; // subtraction let product = x * y; // multiplication let quotient = x / y; // division let remainder = x % y; // modulus (remainder of division)
赋值运算符赋值:
let x = 10; // assign 10 to x x += 5; // add 5 to x (x = x + 5) x *= 2; // multiply x by 2
javascript 有多种数据类型:
let name = "alice"; // string let age = 25; // number let isstudent = true; // boolean let person = {name: "john", age: 30}; // object let colors = ["red", "blue", "green"]; // array let notdefined; // undefined let emptyvalue = null; // null
函数是设计用于执行任务的代码块。
function greet(name) { return "hello " + name; } console.log(greet("alice")); // output: hello alice
javascript 对象是键值对。
let person = { name: "john", age: 30, greet: function() { console.log("hello, " + this.name); } }; person.greet(); // output: hello, john
使用点表示法或方括号表示法访问对象属性:
let car = {make: "toyota", model: "corolla"}; console.log(car.make); // output: toyota console.log(car["model"]); // output: corolla
对象方法是存储为对象属性的函数:
let person = { name: "alice", sayhello: function() { return "hello, " + this.name; } }; console.log(person.sayhello()); // output: hello, alice
对象构造函数允许您创建具有相同结构的多个对象:
function person(name, age) { this.name = name; this.age = age; } let john = new person("john", 30); console.log(john.name); // output: john
这些是 javascript 基础知识的前几个示例,带有解释和代码。如果您想深入了解数组、循环、字符串操作等特定部分,请告诉我!
让我们继续逐步了解 javascript 概念的完整列表以及代码示例:
javascript 可以响应各种用户事件,例如单击、鼠标移动或按键。
<button onclick="displaymessage()">click me</button> <script> function displaymessage() { alert("button clicked!"); } </script>
javascript 中的字符串是字符序列。
let greeting = "hello, world!"; let length = greeting.length; // string length let uppercasegreeting = greeting.touppercase(); // convert to uppercase console.log(uppercasegreeting); // output: hello, world!
javascript 提供了几种内置的字符串方法:
let str = "hello, world!"; let substr = str.substring(null, 5); // extract substring let replacedstr = str.replace("world", "everyone"); // replace part of string console.log(replacedstr); // output: hello, everyone!
您可以在 javascript 字符串中搜索子字符串:
let sentence = "javascript is awesome!"; let position = sentence.indexof("awesome"); // finds the position of the word 'awesome' console.log(position); // output: 15
模板文字允许在字符串中嵌入变量和表达式。
let name = "alice"; let greeting = `hello, ${name}!`; // template literal using backticks console.log(greeting); // output: hello, alice!
javascript 处理整数和浮点数。
let num1 = 5; let num2 = 2.5; let sum = num1 + num2; // adding numbers console.log(sum); // output: 7.5
bigint 用于任意大的整数。
let bignumber = bigint("123456789012345678901234567890"); console.log(bignumber); // output: 123456789012345678901234567890n
javascript 提供了数字方法,例如:
let num = 9.656; let rounded = num.tofixed(2); // rounds to 2 decimal places console.log(rounded); // output: 9.66
javascript 具有内置的数字属性:
let max = number.max_value; // largest possible number in js console.log(max); // output: 1.7976931348623157e+308
数组是值的列表:
let fruits = ["apple", "banana", "cherry"]; console.log(fruits[1]); // output: banana
javascript 数组具有多种方法:
let fruits = ["apple", "banana", "cherry"]; fruits.push("mango"); // adds an element at the end let lastfruit = fruits.pop(); // removes the last element console.log(lastfruit); // output: mango
您可以搜索数组中的值:
let numbers = [1, 2, 3, 4, 5]; let index = numbers.indexof(3); // find the index of 3 console.log(index); // output: 2
您可以使用 sort() 对数组进行排序:
let fruits = ["banana", "apple", "cherry"]; fruits.sort(); // sort alphabetically console.log(fruits); // output: ["apple", "banana", "cherry"]
使用循环或数组方法迭代元素:
let fruits = ["apple", "banana", "cherry"]; fruits.foreach((fruit) => console.log(fruit)); // output each fruit
用 const 声明的数组仍然可以修改其内容:
const fruits = ["apple", "banana"]; fruits.push("cherry"); // this works console.log(fruits); // output: ["apple", "banana", "cherry"]
javascript 提供了 date 对象来处理日期:
let date = new date(); console.log(date); // output: current date and time
您可以用不同的方式格式化日期:
let date = new date(); console.log(date.todatestring()); // output: mon sep 28 2024
javascript 提供了获取部分日期的方法:
let date = new date(); let year = date.getfullyear(); // get the year let month = date.getmonth(); // get the month (0-11) console.log(year, month); // output: 2024, 8 (september)
您可以设置日期的特定部分:
let date = new date(); date.setfullyear(2025); // set the year to 2025 console.log(date); // output: updated date with the new year
javascript 提供了一个用于数学运算的 math 对象:
let x = math.pi; // value of pi let y = math.sqrt(16); // square root of 16 console.log(x, y); // output: 3.141592653589793, 4
您可以在 javascript 中生成随机数:
let randomnum = math.random(); // generates a number between 0 and 1 console.log(randomnum);
布尔值可以有两个值:true 或 false。
let isjavascriptfun = true; let isfishtasty = false; console.log(isjavascriptfun); // output: true
比较运算符用于比较值:
let a = 10; let b = 5; console.log(a > b); // true console.log(a == b); // false console.log(a !== b); // true
条件语句允许您根据条件执行代码:
let age = 18; if (age >= 18) { console.log("you are an adult."); } else { console.log("you are not an adult."); }
switch 语句允许您根据值执行不同的代码块:
let fruit = "apple"; switch (fruit) { case "banana": console.log("banana"); break; case "apple": console.log("apple"); break; default: console.log("unknown fruit"); }
for 循环允许您重复代码指定的次数:
for (let i = 0; i <hr><h3> <strong>45。 js 循环 for in</strong> </h3> <p>for-in 循环迭代对象的属性:</p> <pre class="brush:php;toolbar:false"> let person = {name: "john", age: 30, city: "new york"}; for (let key in person) { console.log(key + ": " + person[key]); }
for-of 循环用于迭代可迭代对象,例如数组:
let fruits = ["apple", "banana", "cherry"]; for (let fruit of fruits) { console.log(fruit); // output each fruit }
只要条件为真,while 循环就会执行:
let i = 0; while (i <hr><h3> <strong>48。 js 休息</strong> </h3> <p>break 用于退出循环:</p> <pre class="brush:php;toolbar:false"> for (let i = 0; i <hr><h3> <strong>49。 js 可迭代</strong> </h3> <p>可以迭代的对象称为可迭代对象,如数组、字符串、映射等。</p> <pre class="brush:php;toolbar:false"> let str = "Hello"; for (let char of str) { console.log(char); // Output each character }
这是 javascript 教程的延续,涵盖下一组概念。如果您还想进一步了解,请告诉我
详细信息或任何说明!