变量和值

  1. var a = 10; // 变量声明并赋值
  2. var x = 1, y = 2;
  3. var bar = 1;
  4. bar = 2;
  5. console.log(bar);

:::info

  1. 变量名只能以$、下划线、字母开头,后面可以是$、下划线、字母、数字。特别注意不能以数字开头。
  2. 不能以js的关键字、保留字作为变量的名称
  3. 变量命名要语义化
  4. 小驼峰命名 testName :::

    基础数据类型

  • Number
  • String
  • Boolean
  • Undefined
  • Null
  • Symbol
  • BigInt

    堆栈内存

    1. var a = 3;
    2. var b = a;
    3. a = 1;
    4. console.log(a,b)
    1. var arr1 = [1,2,3,4]
    2. var arr2 = arr1;
    3. console.log(arr1,arr2)
    4. arr1.push(5)
    5. console.log(arr1,arr2)
    6. arr1 = [1,2]
    7. console.log(arr1,arr2)

    运算和运算符

  • ++运算与—运算

  • 运算符的优先级
  • 字符串比较时:从左到右依次比较ASCII码的大小
  • 比较时,可能会进行数据类型的转换
  • 逻辑运算符:与、或、非的使用
    1. var a = 1 && 2;
    2. console.log(a) // 2
    3. // 第一个为真就返回第二个的值
    4. // 第一个为假就返回第一个的值
    1. var b = 0 || null || 1 || 0
    2. console.log(b)
    3. // 遇到假就往后走,遇到真或者走到最后就返回当前的值

    循环

    break用于终止循环,continue用于跳过某次循环 ```javascript for(var i = 0; i < 10; i++) { console.log(i) }

var j = 0 while(j < 10) { console.log(j) }

// for(声明;判断;操作){}

// do while操作

  1. <a name="iRr28"></a>
  2. ### typeof
  3. ```javascript
  4. // typeof 可以用来检测基本数据类型,返回的结果都是字符串
  5. typeof [] // object
  6. typeof null // object
  7. typeof undefined // undefined
  8. typeof function() {} // function
  9. console.log(a) // ReferenceError
  10. console.log(typeof(a)) // undefined
  11. console.log(typeof(typeof(a))) // string

类型转换

Number

  1. Number()
  2. parseInt(string,radix)
  3. parseFloat()
  4. +'123'
  5. console.log(Number(null)) // 0
  6. console.log(Number(undefined)) // NaN
  7. var a = '10'
  8. console.log(parseInt(a,16)) // 16
  9. parseInt('abc123') // NaN
  10. parseInt('123abc') // 123
  11. parseFloat('3.1415926') // 3.1415926
  12. var num = '3.1415926'
  13. console.log(num.toFixed(2)) // 四舍五入保留两位小数

String

  • toString()
  • String()
  • 字符串拼接 ‘’+

    boolean

    1. 除去nudefinednull0NaNfalse、空字符串转换为false,其他都转换为true

隐式类型转换

  1. var a = '123'
  2. a++;
  3. console.log(a) // 124
  4. // 在关系运算符中,null,undefined都会被Number()转为数字类型
  5. // 相等运算符中,null和undefined都不会转换为数字类型,而是经过特殊处理转化为false
  6. console.log(undefined == 0) // false
  7. console.log(null == 0) // false
  8. console.log(null == undefined) // true

isNaN

  1. isNaN()会进行数据类型的转换,在处理的时候会判断传入的变量能否转换为数字,如果能转换为数字则会返回false,如果无法转换则会返回true
  1. isNaN(undefind) // true
  2. isNaN({}) // true
  3. isNaN('王小波') // true
  4. isNaN(null) // false

函数

函数声明

  1. // 函数声明
  2. function test() {
  3. var a = b = 1;
  4. console.log(a,b)
  5. }
  6. test()
  7. console.log(a,b)
  8. // 函数表达式
  9. var test1 = function test2 () {}
  10. test1()
  11. // 匿名函数表达式
  12. var foo = function() {}

arguments

  1. function foo(a,b) {
  2. console.log(a,b)
  3. }
  4. foo(1,2)
  5. function bar(a,b,c) {
  6. console.log(bar.length) // 获取形参长度
  7. console.log(arguments)
  8. console.log(a,b,c) // 1,2,undefined
  9. }
  10. bar(1,2,3)

参数默认值

  1. function test(a = 1, b =2) {
  2. console.log(a,b)
  3. }
  4. test()
  5. function foo(a,b) {
  6. a = a || 1;
  7. b = b || 2;
  8. }
  9. foo()
  10. function bar(a,b) {
  11. a = typeof a === undefined ? 1 : a;
  12. }