JS
Copy
var : 很少使用,全局作用域
let : 值可以被修改
const : 值不能被修改,而且需要在声明时初始化数值
console .log ('hello world' )
let bbb
bbb = 30
bbb = 31
console .log (bbb)
const aaa = 50
console .log (aaa)
hello world
31
50
数据类型#
值类型 :String、Number、Boolean、Null、Undefined、Symbol
引用类型 :对象、数组、函数、另外两个特殊的对象:正则和日期
Copy
String : 字符串
Number :数字,不区分小数点与整数
Boolean : 布尔
null :定义为空
undefined : 没定义,当不赋值也是没定义
const username = "tcy" ;
const age = 30 ;
const rate = 4.5 ;
const isCool = true ;
const x = null ;
const y = undefined ;
let z;
console .log (typeof rate)
String#
Copy
console .log ("我是:" + username)
console .log (`我是:${username} ` )
const s = 'hello world'
console .log (s.length )
console .log (s.toUpperCase ())
console .log (s.toLowerCase ())
console .log (s.substring (0 ,5 ))
console .log (s.substring (0 ,5 ).toUpperCase ())
console .log (s.split ('' ))
数组为可变类型
更多方法可以查询JavaScript官网
Copy
const fruits = ["apples" ,"orages" ,"pears" ,10 ,true ]
console .log (fruits)
console .log (fruits[1 ])
fruits[1 ] = "apples"
console .log (fruits[1 ])
fruits.push ("mangos" )
console .log (fruits)
fruits.unshift ("strawberries" )
console .log (fruits)
fruits.pop ()
console .log (fruits)
console .log (Array .isArray (fruits))
console .log (fruits.indexOf ("apples" ))
类似于Go的字典,可以对象中保存对象
JavaScript 变量均为对象。当声明一个变量时,就创建了一个新的对象。
Copy
const person = {
firstName : "tang" ,
lastName : "meinv" ,
age :30 ,
hobbies : ["music" ,"movies" ,"sports" ],
address : {
street : "50 main st" ,
city : "Boston" ,
state : "MA" ,
},
}
console .log (person)
console .log (person.firstName )
console .log (person.hobbies [1 ])
console .log (person.address .state )
{
firstName : 'tang' ,
lastName : 'meinv' ,
age : 30 ,
hobbies : [ 'music' , 'movies' , 'sports' ],
address : { street : '50 main st' , city : 'Boston' , state : 'MA' }
}
tang
movies
MA
Copy
const {firstName,lastName} = person
console .log (firstName)
const {address : {city}} = person
console .log (city)
person.email = "json"
对象数组#
Copy
const todos = [
{
id : 1 ,
test : "Take out trash" ,
isCompleted : true ,
},
{
id : 2 ,
test : "Metting with boss" ,
isCompleted : true ,
},
{
id : 1 ,
test : "Dentist appt" ,
isCompleted : false ,
},
]
console .log (todos)
[
{ id : 1 , test : 'Take out trash' , isCompleted : true },
{ id : 2 , test : 'Metting with boss' , isCompleted : true },
{ id : 1 , test : 'Dentist appt' , isCompleted : false }
]
对象数组转变为json
场景:将对象数组传输给服务器,需要转换为json格式
Copy
const todos = [
{
id : 1 ,
test : "Take out trash" ,
isCompleted : true ,
},
{
id : 2 ,
test : "Metting with boss" ,
isCompleted : true ,
},
{
id : 1 ,
test : "Dentist appt" ,
isCompleted : false ,
},
]
const todoJSON = JSON .stringify (todos)
console .log (todoJSON)
if 判断#
Copy
const x = 10
if (x == "10" ) {
console .log ("x is 10" )
}
if (x === "10" ) {
console .log ("x is 10" )
} else if (x > 10 ) {
console .log ("x is greater than 10" )
}else {
console .log ("x is not 10" )
}
const a = 10
const b = 20
if (a > 8 || b < 19 ) {
console .log ("a is more than 10 or b is more than 19" )
}
if (a > 9 && b > 10 ) {
console .log ("a is more than 10 or b is more than 19" )
}
三目运算符#
Copy
const d = 10 ;
const color = d > 5 ? "red" : "blue"
console .log (color)
Switch#
Copy
const d = 10 ;
const color = d > 5 ? "red" : "blue"
console .log (color)
switch (color) {
case "red" :
console .log ("color is red" );
break
case "blue" :
console .log ("color is blue" );
break
default :
console .log ("color is Not red or blue" );
}
for & While#
Copy
for (let i = 0 ;i < 10 ; i++) {
console .log (i)
}
for (let i = 0 ;i <= 10 ; i++) {
console .log (i)
}
const todos = [
{
id : 1 ,
test : "Take out trash" ,
isCompleted : true ,
},
{
id : 2 ,
test : "Metting with boss" ,
isCompleted : true ,
},
{
id : 1 ,
test : "Dentist appt" ,
isCompleted : false ,
},
]
for (let i = 0 ;i < todos.length ;i++) {
console .log (todos[i].test )
}
for (let todo of todos) {
console .log (todo.test )
}
let i = 0 ;
while (i < 10 ) {
console .log (`While Loop Number: ${i} ` );
i++
}
关键词 function 必须是小写的,并且必须以与函数名称相同的大小写来调用函数。
Copy
function myFunction ( ) {
console .log ("hello world" )
}
myFunction ()
Copy
function argFunction (arg1,arg2 ) {
console .log ("Welcome " + arg1 + ", the " + arg2)
}
argFunction ('tcy' ,'job' )
函数返回值
"demo" 元素的 innerHTML 将是12
Copy
function myFunction (a,b ) {
return a * b;
}
document .getElementById ("demo" ).innerHTML =myFunction (4 ,3 );
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!