首页 > web前端 > js教程 > 正文

Javascript重要概念||重要的 JavaScript 概念

心靈之曲
发布: 2024-10-08 08:41:50
转载
646人浏览过

javascript重要概念||重要的 javascript 概念

逐步使用代码示例来帮助您更好地理解每个概念。


1. js简介

javascript 是一种多功能语言,可以在浏览器或服务器上运行(使用 node.js)。它用于使网页具有交互性。


<!doctype html>
<html>
<body>
  <h2>hello, javascript!</h2>
  <button onclick="myfunction()">click me</button>

  <script>
    function myfunction() {
      alert("hello, welcome to javascript!");
    }
  </script>
</body>
</html>


登录后复制

2. js 去哪里

javascript 可以放置在三个位置:

  1. 内联: ``html

点我

立即学习Java免费学习笔记(深入)”;

2. **internal** (inside a `<script>` tag):
```html


<script>
  function showmessage() {
    alert("internal js");
  }
</script>


登录后复制
  1. 外部(在外部 .js 文件中): ``html

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>


登录后复制

4. js 语句

声明是一条指令。例如:


let a = 5;    // this is a statement
let b = 6;    // this is another statement
let sum = a + b;  // statement calculating the sum


登录后复制

5. js 语法

语法是指 javascript 程序如何构建的一组规则:


let name = "john";  // variable declaration and assignment
console.log(name);  // statement to output the value of 'name'


登录后复制

6. js评论


// this is a single-line comment

/* 
   this is a 
   multi-line comment
*/

let x = 10; // this is an inline comment


登录后复制

7. js 变量

变量存储数据值。您可以使用 let、var 或 const 来声明它们。


let name = "alice";   // declaring a string variable
var age = 30;         // declaring a number variable
const country = "usa"; // declaring a constant


登录后复制

8. js 让

let 关键字允许您声明具有块作用域的变量。


let x = 10;
if (x > 5) {
  let y = 20; // 'y' only exists inside this block
}
console.log(y); // error: y is not defined


登录后复制

9. js 常量

用 const 声明的变量是不可变的(不能重新分配它们)。


const pi = 3.14159;
pi = 3.14; // error: assignment to constant variable.


登录后复制

10。 js 运算符

运算符用于对变量和值执行运算:


let a = 10;
let b = 20;

let sum = a + b;  // arithmetic operator
let isequal = (a == b);  // comparison operator
let notequal = (a != b);  // logical operator


登录后复制

11。 js 算术

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)


登录后复制

12。 js 作业

赋值运算符赋值:


let x = 10;  // assign 10 to x
x += 5;  // add 5 to x (x = x + 5)
x *= 2;  // multiply x by 2


登录后复制

13。 js 数据类型

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


登录后复制

14。 js 函数

函数是设计用于执行任务的代码块。


function greet(name) {
  return "hello " + name;
}

console.log(greet("alice"));  // output: hello alice


登录后复制

15。 js 对象

javascript 对象是键值对


let person = {
  name: "john",
  age: 30,
  greet: function() {
    console.log("hello, " + this.name);
  }
};

person.greet();  // output: hello, john


登录后复制

16。 js 对象属性

使用点表示法或方括号表示法访问对象属性:


let car = {make: "toyota", model: "corolla"};
console.log(car.make);  // output: toyota
console.log(car["model"]);  // output: corolla


登录后复制

17。 js 对象方法

对象方法是存储为对象属性的函数:


let person = {
  name: "alice",
  sayhello: function() {
    return "hello, " + this.name;
  }
};

console.log(person.sayhello());  // output: hello, alice


登录后复制

18。 js 对象构造函数

对象构造函数允许您创建具有相同结构的多个对象:


function person(name, age) {
  this.name = name;
  this.age = age;
}

let john = new person("john", 30);
console.log(john.name);  // output: john


登录后复制

这些是 javascript 基础知识的前几个示例,带有解释和代码。如果您想深入了解数组循环字符串操作等特定部分,请告诉我!

让我们继续逐步了解 javascript 概念的完整列表以及代码示例:


19。 js 事件

javascript 可以响应各种用户事件,例如单击、鼠标移动或按键。


<button onclick="displaymessage()">click me</button>

<script>
  function displaymessage() {
    alert("button clicked!");
  }
</script>


登录后复制

20。 js 字符串

javascript 中的字符串是字符序列。


let greeting = "hello, world!";
let length = greeting.length;  // string length
let uppercasegreeting = greeting.touppercase(); // convert to uppercase
console.log(uppercasegreeting);  // output: hello, world!


登录后复制

21。 js 字符串方法

javascript 提供了几种内置的字符串方法:


let str = "hello, world!";
let substr = str.substring(0, 5);  // extract substring
let replacedstr = str.replace("world", "everyone");  // replace part of string
console.log(replacedstr);  // output: hello, everyone!


登录后复制

22。 js 字符串搜索

您可以在 javascript 字符串中搜索子字符串:


let sentence = "javascript is awesome!";
let position = sentence.indexof("awesome");  // finds the position of the word 'awesome'
console.log(position);  // output: 15


登录后复制

23。 js 字符串模板

模板文字允许在字符串中嵌入变量和表达式。


let name = "alice";
let greeting = `hello, ${name}!`;  // template literal using backticks
console.log(greeting);  // output: hello, alice!


登录后复制

24。 js 数字

javascript 处理整数和浮点数。


let num1 = 5;
let num2 = 2.5;
let sum = num1 + num2;  // adding numbers
console.log(sum);  // output: 7.5


登录后复制

25。 js bigint

bigint 用于任意大的整数。


let bignumber = bigint("123456789012345678901234567890");
console.log(bignumber);  // output: 123456789012345678901234567890n


登录后复制

26。 js 数字方法

javascript 提供了数字方法,例如:


let num = 9.656;
let rounded = num.tofixed(2);  // rounds to 2 decimal places
console.log(rounded);  // output: 9.66


登录后复制

27。 js 数字属性

javascript 具有内置的数字属性:


let max = number.max_value;  // largest possible number in js
console.log(max);  // output: 1.7976931348623157e+308


登录后复制

28。 js 数组

数组是值的列表:


let fruits = ["apple", "banana", "cherry"];
console.log(fruits[1]);  // output: banana


登录后复制

29。 js 数组方法

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


登录后复制

30。 js 数组搜索

您可以搜索数组中的值:


let numbers = [1, 2, 3, 4, 5];
let index = numbers.indexof(3);  // find the index of 3
console.log(index);  // output: 2


登录后复制

31。 js 数组排序

您可以使用 sort() 对数组进行排序:


let fruits = ["banana", "apple", "cherry"];
fruits.sort();  // sort alphabetically
console.log(fruits);  // output: ["apple", "banana", "cherry"]


登录后复制

32。 js 数组迭代

使用循环或数组方法迭代元素:


let fruits = ["apple", "banana", "cherry"];
fruits.foreach((fruit) => console.log(fruit));  // output each fruit


登录后复制

33。 js 数组常量

用 const 声明的数组仍然可以修改其内容:


const fruits = ["apple", "banana"];
fruits.push("cherry");  // this works
console.log(fruits);  // output: ["apple", "banana", "cherry"]


登录后复制

34。 js 日期

javascript 提供了 date 对象来处理日期:


let date = new date();
console.log(date);  // output: current date and time


登录后复制

35。 js 日期格式

您可以用不同的方式格式化日期:


let date = new date();
console.log(date.todatestring());  // output: mon sep 28 2024


登录后复制

36。 js 日期获取方法

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)


登录后复制

37。 js 日期设置方法

您可以设置日期的特定部分:


let date = new date();
date.setfullyear(2025);  // set the year to 2025
console.log(date);  // output: updated date with the new year


登录后复制

38。 js 数学

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


登录后复制

39。 js随机

您可以在 javascript 中生成随机数:


let randomnum = math.random();  // generates a number between 0 and 1
console.log(randomnum);


登录后复制

40。 js 布尔值

布尔值可以有两个值:true 或 false。


let isjavascriptfun = true;
let isfishtasty = false;
console.log(isjavascriptfun);  // output: true


登录后复制

41。 js 比较

比较运算符用于比较值:


let a = 10;
let b = 5;
console.log(a > b);  // true
console.log(a == b);  // false
console.log(a !== b);  // true


登录后复制

42。 js if else

条件语句允许您根据条件执行代码:


let age = 18;
if (age >= 18) {
  console.log("you are an adult.");
} else {
  console.log("you are not an adult.");
}


登录后复制

43。 js 切换

switch 语句允许您根据值执行不同的代码块:


let fruit = "apple";

switch (fruit) {
  case "banana":
    console.log("banana");
    break;
  case "apple":
    console.log("apple");
    break;
  default:
    console.log("unknown fruit");
}


登录后复制

44。 js 循环

for 循环允许您重复代码指定的次数:


for (let i = 0; i < 5; i++) {
  console.log(i);  // output: 0, 1, 2, 3, 4
}


登录后复制

45。 js 循环 for in

for-in 循环迭代对象的属性:


let person = {name: "john", age: 30, city: "new york"};
for (let key in person) {
  console.log(key + ": " + person[key]);
}


登录后复制

46。 js 循环 for

for-of 循​​环用于迭代可迭代对象,例如数组:


let fruits = ["apple", "banana", "cherry"];
for (let fruit of fruits) {
  console.log(fruit);  // output each fruit
}


登录后复制

47。 js 循环 while

只要条件为真,while 循环就会执行:


let i = 0;
while (i < 5) {
  console.log(i);  // output: 0, 1, 2, 3, 4
  i++;
}


登录后复制

48。 js 休息

break 用于退出循环:


for (let i = 0; i < 5; i++) {
  if (i === 3) break;
  console.log(i);  // output: 0, 1, 2
}


登录后复制

49。 js 可迭代

可以迭代的对象称为可迭代对象,如数组、字符串、映射等。


let str = "Hello";
for (let char of str) {
  console.log(char);  // Output each character
}


登录后复制

这是 javascript 教程的延续,涵盖下一组概念。如果您还想进一步了解,请告诉我

详细信息或任何说明!

以上就是Javascript重要概念||重要的 JavaScript 概念的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号