JavaScript的两大类内建数据类型

开发 前端
JavaScript的数据类型在大的方向上分为两类:1)primitive types and 2)object tyeps。

[[360739]]

 JavaScript的数据类型在大的方向上分为两类:1)primitive types and 2)object tyeps。

其一 primitive types包括常规的 numbers,string, booleans 以及特殊类型的 null 和 undefined。而且以上五类都是immutuable types;

其二,object types 包括object,以及特殊类型的object即array。其他比如 Set,Map,typed array, RegExp and Date types.

一、Numbers

Numeric literal 表示 十六进制,二进制和八进制:

  1. //integer literals 
  2. > 0xff 
  3. 255 
  4. > 0b1011 
  5. 11 
  6. > 0o377 
  7. 255 
  8. > 377 
  9. 377 
  10. //floating-point literals 
  11. undefined 
  12. > 6.02e23 
  13. 6.02e+23 
  14. > 1.47e-23 
  15. 1.47e-23 
  16. //Arithmetic  
  17. Math.hypo 
  18.  
  19. //Infinity 
  20. Infinity                    // A positive number too big to represent 
  21. Number.POSITIVE_INFINITY    // Same value 
  22. 1/0                         // => Infinity 
  23. Number.MAX_VALUE * 2        // => Infinity; overflow 
  24.  
  25. -Infinity                   // A negative number too big to represent 
  26. Number.NEGATIVE_INFINITY    // The same value 
  27. -1/0                        // => -Infinity 
  28. -Number.MAX_VALUE * 2       // => -Infinity 
  29.  
  30. NaN                         // The not-a-number value 
  31. Number.NaN                  // The same value, written another way 
  32. 0/0                         // => NaN 
  33. Infinity/Infinity           // => NaN 
  34.  
  35. Number.MIN_VALUE/2          // => 0: underflow 
  36. -Number.MIN_VALUE/2         // => -0: negative zero 
  37. -1/Infinity                 // -> -0: also negative 0 
  38. -0 
  39.  
  40. // The following Number properties are defined in ES6 
  41. Number.parseInt()       // Same as the global parseInt() function 
  42. Number.parseFloat()     // Same as the global parseFloat() function 
  43. Number.isNaN(x)         // Is x the NaN value? 
  44. Number.isFinite(x)      // Is x a number and finite? 
  45. Number.isInteger(x)     // Is x an integer
  46. Number.isSafeInteger(x) // Is x an integer -(2**53) < x < 2**53? 
  47. Number.MIN_SAFE_INTEGER // => -(2**53 - 1) 
  48. Number.MAX_SAFE_INTEGER // => 2**53 - 1 
  49. Number.EPSILON          // => 2**-52: smallest difference between numbers 
  50. // 浮点数  
  51. 18,437,736,874,454,810,627 只有这么多浮点数,能被表示出来。 
  52. // rouding problems  
  53. //BigInt 
  54.  
  55. //Date and time 
  56. let timestamp = Date.now();  // The current time as a timestamp (a number). 
  57. let now = new Date();        // The current time as a Date object. 
  58. let ms = now.getTime();      // Convert to a millisecond timestamp
  59. let iso = now.toISOString(); // Convert to a string in standard format. 

 二、String and Text

  1. // 1.string literals 
  2. // 2.escape sequences  
  3. // 3.string methods 
  4. // 4.template literals (tagged template literals) 
  5. // 5.Pattern Matching  
  6. /[1-9][0-9]*/;  

 三、Boolean Values

只有 true 和 false 这两项。

四、null and undefined

  1. > typeof(null
  2. 'object' 

 五、Symbols

  1. let s = Symbol.for("shared"); 
  2. let t = Symbol.for("shared"); 
  3. s === t          // => true 
  4. s.toString()     // => "Symbol(shared)" 
  5. Symbol.keyFor(t) // => "shared" 

 六、Global

  • Global constants like undefined, Infinity, and NaN
  • Global functions like isNaN(), parseInt() (§3.9.2), and eval() (§4.12)
  • Constructor functions like Date(), RegExp(), String(), Object(), and Array() (§3.9.2)
  • Global objects like Math and JSON (§6.8)

七、Immutable Primitives and Mutable Object Referece

  1. function equalArray(a, b) { 
  2. ... if (a === b) return true
  3. ... if (a.length !== b.length) return false
  4. ... for (let i = 0; i < a.length; i++) { 
  5. ..... if (a[i] !== b[i]) return false
  6. ..... } 
  7. ... return true
  8. ... } 

 八、Type Conversions

implicite conversion and explicite conversions

九、Variable Declaration and Assignment

  1. // Destructuring Assignment 
  2. [x,y] = [x+1,y+1];  // Same as x = x + 1, y = y + 1 
  3. [x,y] = [y,x];      // Swap the value of the two variables 
  4. // Same as const sin=Math.sin, cos=Math.cos, tan=Math.tan 
  5. const {sin, cos, tan} = Math; 
  6. //此处与python的用法完全一致。 

 【编辑推荐】

 

责任编辑:姜华 来源: 今日头条
相关推荐

2010-09-26 16:04:48

JVM内存溢出

2010-05-07 16:13:07

Oracle归档模式

2010-06-11 17:01:09

Windows Pho

2010-06-09 10:38:07

UML类图

2016-08-18 14:13:55

JavaScript基本数据引用数据

2022-03-07 13:58:30

JavaScript原始数据前端

2011-07-29 10:12:12

JavaScript

2015-10-22 10:44:50

2021-12-03 15:24:45

Javascript数据类型

2010-10-08 15:11:28

JavaScript数

2018-11-15 09:45:47

JavaScript数据类型变量

2019-09-28 22:41:18

OracleMySQL隐式数据

2010-10-08 09:02:03

JavaScript基

2017-02-27 08:34:09

JavaScript数据引用

2021-10-13 09:50:02

鸿蒙HarmonyOS应用

2019-08-12 11:40:48

数据库SQLite3数据类型

2010-09-17 16:18:43

Java内存溢出

2010-08-30 10:03:13

2021-02-25 07:08:30

JavaScript 前端面试题

2022-08-12 16:12:34

JavaScript数据类型字符串
点赞
收藏

51CTO技术栈公众号