Blogger Information
Blog 47
fans 3
comment 0
visits 38099
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js常用数据类型,变量和常量的声明与赋值
Original
778 people have browsed it

js常用数据类型,变量和常量的声明与赋值

1.js的常用数据类型

原始类型:值传递、数值、字符串、布尔、undefind、null、symbol

  • 值传递
  1. <script>
  2. // 值传递
  3. let a = 100;
  4. b = a;
  5. console.log(b);
  6. </script>

演示截图:

  • 数值
    1. <script>
    2. // 数值
    3. let age = 28;
    4. console.log(age, typeof age);
    5. </script>
    演示截图:

  • 字符串

    1. <script>
    2. // 字符串
    3. let mail = 'admin@php.cn';
    4. console.log(mail, typeof mail);
    5. </script>

    演示截图:

  • 布尔

    1. <script>
    2. // 布尔
    3. let isMarried = true;
    4. console.log(isMarried, typeof isMarried);
    5. </script>

    演示截图:

  • undefined:未初始化变量的默认值

    1. <script>
    2. // undefined
    3. let user;
    4. console.log(user, typeof user);
    5. </script>

    演示截图:

  • null:空对象

    1. <script>
    2. let obj = null;
    3. console.log(obj, typeof null);
    4. </script>

    演示截图:

  • Symbol:符号,创建空对象属性的唯一标识

    1. <script>
    2. // 符号,创建空对象属性的唯一标识
    3. let s = Symbol('symbol');
    4. console.log(s, typeof s);
    5. </script>

    演示截图:

引用类型:引用传递、对象、数组、函数

  • 对象

    1. <script>
    2. let user = {
    3. id: 1,
    4. name: '张三',
    5. 'my email': 'mail@email.com',
    6. getName: function () {
    7. return '我的名字:' + this.name;
    8. }
    9. }
    10. console.log(user.id, user.name, user['my email']);
    11. console.log(user.getName());
    12. </script>

    演示截图:

  • 数组:数组中的索引都是从0开始的,按索引来访问元素

    1. <script>
    2. const arr = ['张三', '李四', '王五'];
    3. console.log(arr[0]);
    4. </script>

    演示截图:

  • 函数
    1. <script>
    2. function hello() {
    3. console.log(test);
    4. }
    5. console.log(hello, typeof hello);
    6. </script>
    演示截图:

2. 变量与常量的声明与赋值

let变量 const常量

  • let变量与赋值
  1. <script>
  2. // 声明:let禁止重复声明
  3. let user;
  4. // 赋值
  5. user = '张三';
  6. console.log(user);
  7. </script>

演示截图:

  • const常量与赋值
  1. <script>
  2. const user = 'admin';
  3. console.log(user);
  4. </script>

演示截图:

常量是特殊的变量:只读变量
常量声明后不能删除,也不能更新
常量的声明与初始化必须同步完成
实际工作中尽可能的首选const常量,其次才考虑let

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments