1.声明变量

变量它会在内存中占据一定的空间

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <!-- 1.浏览器环境 -->
  10. <!-- 2.服务器 -->
  11. <script>
  12. // 变量它会在内存中占据一定的空间
  13. // es5
  14. var a = 10;
  15. // es6
  16. let b = 20;
  17. console.log(a);
  18. </script>
  19. </body>
  20. </html>

2.变量的命名规则

python,javascript,ruby,php
C,C++,C#,Java,Typescript,Dart 强类型(了解)
只有命名语义话就不会出问题
void,class,static(关键字和保留字)不用记

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // python,javascript,ruby,php
        // C,C++,C#,Java,Typescript,Dart  强类型(了解)
        // 只有命名语义话就不会出问题
        // void,class,static(关键字和保留字)不用记
        var a;
        a= "hello world";
        // 赋值的时候可以给任意类型
    </script>
</body>
</html>

3.js中的数据类型

基本类型,复杂类型(引用类型) 按其在内存中的位置进行区分的—>
基本类型

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- 基本类型,复杂类型(引用类型) 按其在内存中的位置进行区分的-->
    <!-- 基本类型  -->
    <script>
        // 0-1
        // number,string,boolean
        // typeof操作符去判断一个数据的类型
        var num  = 10;
        var b = 10.12;
        console.log(typeof num);
        console.log(typeof b);
        // 使用单引号,或者双引号包裹的就是string
        // 在javascipt中声明一个变量不赋值,会输出undefined
        var str = "hello world";
        var s = 'hello'
        console.log(typeof str)
        console.log(typeof s);
        // boolean true,false
        var c = true;
        var d = false;
        console.log(typeof c);
        console.log(typeof d);

    </script>
</body>
</html>

4.变量重复声明

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var a = 10;
        var a = 20;
        console.log(a);
    </script>
</body>
</html>

5.声明提前

js在执行代码的时候,会将所有使用var声明的变量,放置在作用域的顶层集中创建,
赋值会留在原地

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        /*  声明提前 
        js在执行代码的时候,会将所有使用var声明的变量,放置在作用域的顶层集中创建,
        赋值会留在原地
        */

        console.log(a);
        var a=10;
        var b=20;
    </script>
</body>
</html>

6.引用数据提前

typeof 不能识别数组 object
Array.isArray 输出的是boolean
属性 length 可以读取数组的长度
数组的下标从0开始
下标越界之后输出undefined

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // typeof 不能识别数组  object
        //数组 []  
        // Array.isArray  输出的是boolean
        var arr = [1,2,3];
        var all = ["html","css","js"];
        console.log(typeof arr);
        console.log(Array.isArray(arr));
        // 属性 length  可以读取数组的长度
        console.log(arr.length)
        // 数组的下标从0开始  

        console.log(all[0])
        // 下标越界之后输出undefined
        console.log(arr[100])
    </script>
</body>
</html>

7.获取数组数据

length-1
获取数组最后面的数据
console.log(arr[arr.length-1]);

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var arr = ["html","css","javascript"];
        console.log(arr.length);
        // length-1
        // 获取数组最后面的数据
        console.log(arr[arr.length-1]);
    </script>
</body>
</html>