JavaScript 基础

用法

JavaScript 可以通过不同的方法来输出数据

使用 console.long() 写入到浏览器的控制台

使用 document.write() 方法将内容写到HTML文件中

使用 innerHTML 写入到HTML元素

使用 window.alert() 弹出警告框

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript 显示数据</title>
</head>
<body>
    <span id="msg"></span>
</body>
</html>
<script>
    // 写入到浏览器的控制台
    window.console.log('111')
    // 将内容写到HTML文件中
    window.document.write('222')
    // 写入到HTML元素
    // 1.找到元素
    // 2.写入到元素
    window.document.querySelector('#msg').innerHTML = '活着';
    // 弹出警告框
    window.alert('道生一 一生二')
</script>

弹窗

 

 变量与常量

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>变量与常量</title>
</head>
<body>
    
</body>
</html>
<script>
    // 变量声明
    let a;
    // 变量赋值
    a =10;
    window.document.write('a= ' + a + '<br>');
    // 变量声明并赋值
    let b=20;
    window.document.write('b= ' + b + '<br>');
    let c=30,d=40;
    window.document.write('c= ' + c + '<br>');
    window.document.write('d= ' + d + '<br>');
    let e,f =50;
    // e声明但没赋值
    window.document.write('e= ' + e + '<br>');
    window.document.write('f= ' + f + '<br>');
    let h;
    window.document.write('h= ' + h + '<br>');
    f = 60;
    window.document.write('f= ' + f + '<br>');
    window.document.write('<hr>');
    const j = 70;
    window.document.write('j= ' + j + '<br>');
    // Uncaught TypeError: Assignment to constant variable.
    // 常量是不能变值的
    j = 80;
</script>

 字符串

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>字符串</title>
</head>
<body>
    
</body>
</html>
<script>
    // 转义字符
    window.document.writeln('\'');
    window.document.writeln("\"");
    window.document.writeln("'");
    window.document.writeln('"');
    let a='我爱你';
    let b='我爱你吗';
    window.document.writeln(a+b);
    let slogan="我真的爱你";
    // 字符串拼接
    window.document.writeln("<br>slogan >>>" +slogan);
    // 字符串模板
    window.document.writeln(`<br>slogan >>> ${slogan}`);
</script>

 基本数据类型

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>基本数据类型</title>
</head>
<body>
    
</body>
</html>
<script>
    // number 数值型
    let a = 10;
    let b = 3.14;
    window.document.writeln('a 的数据类型 >>>'+typeof (a)+'<br>');
    window.document.writeln('b 的数据类型 >>>'+typeof (b)+'<br>');
    // string 字符串类型
    let c ='李';
    let slogan='嘿嘿嘿';
    window.document.writeln('c 的数据类型 >>>'+typeof (c)+'<br>');
    window.document.writeln('slogan 的数据类型 >>>'+typeof (slogan)+'<br>');
    // boolean 布尔类型 true false
    let d=true;
    let e=false;
    window.document.writeln('d 的数据类型 >>>'+typeof (d)+'<br>');
    window.document.writeln('e 的数据类型 >>>'+typeof (e)+'<br>');
    window.document.writeln('ture 的数据类型 >>>'+typeof (true)+'<br>');
    window.document.writeln('false 的数据类型 >>>'+typeof (false)+'<br>');
    // NaN Not an Number
    let result = 20/10;
    window.document.writeln('result 的数据类型 >>>'+typeof (result)+'<br>');
    let result01 = '李'/10;
    window.document.writeln('李/10'+ result01+ '<br>');
    window.document.writeln('李/10 的数据类型 >>>'+typeof (result01)+'<br>');
    // underfined 没有赋值的变量
    let f;
    window.document.writeln('f ='+ f+ '<br>');
    window.document.writeln('f的数据类型 >>>'+typeof (f)+'<br>');
</script>

 数学计算

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数学计算</title>
</head>
<body>
    
</body>
</html>
<script>
    let a=10;
    let b=20;
    window.document.writeln('a + b ='+(a+b)+'<br>');
    window.document.writeln('a - b ='+(a-b)+'<br>');
    window.document.writeln('a * b ='+(a*b)+'<br>');
    window.document.writeln('a / b ='+(a/b)+'<br>');
    window.document.writeln('20 % 3 ='+(20 %3)+'<br>');
</script>

 逻辑运算符

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>逻辑运算符</title>
</head>
<body>
    
</body>
</html>
<script>
    let a = 10;
    let b = 10;
    // 值
    window.document.writeln("a == b >>>" + (a == b)+'<br>');
    // 内存地址
    window.document.writeln("a === b >>>" + (a === b)+'<br>');
    window.document.writeln("a != b >>>" + (a != b)+'<br>');
    let c = true;
    let d = false;
    window.document.writeln("ture == ture >>>" + (ture == flase)+'<br>');
    window.document.writeln("ture != flase >>>" + (ture == flase)+'<br>');
    window.document.writeln("ture != flase >>>" + (ture && flase)+'<br>');
    window.document.writeln("ture != flase >>>" + (ture || flase)+'<br>');
</script>

 选择结构

<script>
    // if(条件表达式){代码块}
    // 如果条件返回结果为true则执行代码块,如果条件返回结果为false则不执行代码块
    let flag = true;
    if (flag) {
        window.document.writeln('<h1>flag的值为true<h1>');
    }
    window.document.writeln('<h1>第一次判断结束<h1>');
    if (flag) {
        window.document.writeln('<h1>flag的值为true<h1>');
    } else {
        window.document.writeln('<h1>flag的值为false<h1>');
    }
    window.document.writeln('<hr>');
    let weekDay = 10 % 7;
    if (weekDay == 1) {
        window.document.writeln('今天是星期一');
    } else if (weekDay == 2) {
        window.document.writeln('今天是星期二');
    } else if (weekDay == 3) {
        window.document.writeln('今天是星期三');
    } else if (weekDay == 4) {
        window.document.writeln('今天是星期四');
    } else if (weekDay == 5) {
        window.document.writeln('今天是星期五');
    } else if (weekDay == 6) {
        window.document.writeln('今天是星期六');
    } else {
        window.document.writeln('今天是星期日');
    }
</script>

 分支结构

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>分支结构</title>
</head>
<body>
    
</body>
</html>
<script>
    let weekDay =10 %7;
    if (weekDay == 1){
        window.document.writeln('今天是星期一');
    }else if(weekDay == 2){
        window.document.writeln('今天是星期二');
    }else if(weekDay == 3){
        window.document.writeln('今天是星期三');
    }else if(weekDay == 4){
        window.document.writeln('今天是星期四');
    }else if(weekDay == 5){
        window.document.writeln('今天是星期五');
    }else if(weekDay == 6){
        window.document.writeln('今天是星期六');
    }else if(weekDay == 7){
        window.document.writeln('今天是星期日');
    }
</script>

 while循环

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>while循环</title>
</head>
<body>
    
</body>
</html>
<script>
    let count = 0;0
    while(count<10){
        window.document.writeln('1111<br>');
        count++;
    }
</script>

while循环 _break

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>while循环</title>
</head>
<body>
    
</body>
</html>
<script>
    let count = 0;0
    while(count<10){
        if(count==5){
            break;
        }
        window.document.writeln('1111<br>');
        count++;
    }
</script>

 while循环 _continue

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>while循环</title>
</head>
<body>
    
</body>
</html>
<script>
    let count = 0;
    while(count<10){
        if(count ++ % 2 == 0){
            continue;
        }
        // 2 4 6 8 10
        window.document.writeln(`第 ${count} 次说爱你<br>`);
        count++; // 可以不写
    }
    window.document.writeln(`<hr>`);
    let count01 = 0;
    while(count01<10){
        if(++count01 % 2 == 0){
            continue;
        }
        // 1 3 5 7 9
        window.document.writeln(`第 ${count01} 次说爱你<br>`);
        count++;
    }
</script>

 for循环

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>for循环</title>
</head>
<body>
    
</body>
</html>
<script>
    for(
        let index = 0;
        index<10;
        index++
    ){
        window.document.writeln(`第${index+1} 此说爱你<br>`)
    }
</script>

 for循环_break

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>for循环</title>
</head>
<body>
    
</body>
</html>
<script>
    for(let index = 0;index<10;index++){
    if(index ==5){
        break;
    }
    window.document.writeln(`第${index+1} 此说爱你<br>`)
}
</script>

 for循环_continue

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>for循环</title>
</head>
<body>
    
</body>
</html>
<script>
    for(let index = 0;index<10;index++){
    if(index %2==0){
        continue;
    }
    window.document.writeln(`第${index+1} 此说爱你<br>`)
}
</script>

 数组

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数组</title>
</head>
<body>
    
</body>
</html>
<script>
    let arr0=['1','2','3'];
    window.console.log(arr0);
    window.console.log(arr0[0]);
    window.console.log(arr0[1]);
    window.console.log(arr0[2]);
    window.console.log(arr0[3]);
    arr0[3]='4';
    window.console.log(arr0[3]);
    window.console.log(arr0.length);
    window.console.log(arr0);
    window.console.log('******************************************************');
    for(let index = 0;index<arr0.length;index++){
        const element=arr0[index];
        window.console.log(element);
    }
    window.console.log('******************************************************');
    arr0.forEach(element => {
        window.console.log(element);
    });
    window.console.log('******************************************************');
    let arr1 = new Array(5);
    window.console.log(arr1);
    for(let index = 0;index<arr1.length;index++){
        arr1[index] = index*3;
    }
    window.console.log(arr1);
    window.console.log('******************************************************');
    let arr2 = new Array(5);
    window.console.log(arr1);
    for(let index = 0;index<arr2.length;index++){
        arr2[index] = index*5;
    }
    window.console.log(arr2);
    window.console.log('******************************************************');
</script>

 数组遍历

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数组遍历</title>
</head>
<body>
    
</body>
</html>
<script>
    let array=['1','2','3'];
    for (let index = 0; index < array.length; index++) {
        const element = array[index];
        window.console.log(element);
    }
    window.console.log('********************************************************');
    array.forEach(element =>{
        window.console.log(element);
    });
    window.console.log('********************************************************');
    for(const index in array){
        window.console.log(index);
    }
    window.console.log('********************************************************');
    for(const index in array){
        window.console.log(array[index]);
    }
    window.console.log('********************************************************');
    for(const element in array){
        window.console.log(element);
    }
    window.console.log('********************************************************');
    window.console.log('********************************************************');
</script>

列表生成式

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>列表生成式</title>
</head>
<body>
    
</body>
</html>
<script>
    let nums = [0, 2, 5, 6, 8];
    let res01 = nums.every(num =>{
       return num % 2 == 0;
    });
    console.log(res01);
    let res02 = nums.filter(num => num % 2 == 0);
    console.log(res02);
    let res03 = nums.map(num => num * 2);
    console.log(res03);
    let res04 = nums.reduce((a, b) => a + b);
    console.log(res04);
    let res05 = nums.join('-');
    console.log(res05);
    let strs = ['李','四','七'];
    let res06 = strs.join('');
    console.log(res06);
    let times = [1983,11,22];
    let date = times.join('-');
    console.log(date);
</script>

 

 数组新增和删除元素

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数组新增和删除元素</title>
</head>
<body>
    
</body>
</html>
<script>
    let array = ['java','python'];
    window.console.log(array);
    // 数组末尾追加元素
    array[array.length] = 'c#';
    window.console.log(array);
    array.push('php');
    window.console.log(array);
    // 在数组起始位插入
    array.unshift('scala');
    window.console.log(array);
    // 以弹栈的方式获取数组最大下标位置上的元素
    let a = array.pop();
    window.console.log(a);
    window.console.log(array);
    // 以弹栈的方式获取数组最小下标位置上的元素
    let b = array.shift();
    window.console.log(b);
    window.console.log(array);
</script>

 数组截取slice

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数组截取slice</title>
</head>
<body>
    
</body>
</html>
<script>
    let array =[0,1,2,3,4,5,6,7,8,9];
    console.log(array);
    let newArr = array.slice(3,7);
    console.log(newArr);
    console.log(array);
</script>

 数组截取splice

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数组截取splice</title>
</head>
<body>
    
</body>
</html>
<script>
    let array =[0,1,2,3,4,5,6,7,8,9];
    console.log(array);
    let newArr = array.splice(3,2);
    console.log(newArr);
    console.log(array);
    newArr = array.splice(3,2,'李','4');
    console.log(newArr);
    console.log(array);
</script>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值