JavaScript从零开始 | 01

此篇笔记为从零开始学习JavaScript的第一篇。作为我系统性学习JavaScript的开始。此系列将会以英文为主。主要学习资料为Mozilla的Documentation。

JavaScript Building Blocks: Types

  • Number
  • String
  • Boolean
  • Function
  • Object
  • Symbol (new in ES2015,也就是ES6)
    除了以上基础Types之外,JavaScript同时支持使用nullundefined, 以及ArrayDateRegExp。因此,一种更正确的Types分类形式为:
  • Number
  • String
  • Boolean
  • Symbol
  • Object
    • Function
    • Array
    • Date
    • RegExp
  • null
  • undefined

Numbers

JavaScript是不支持纯所谓Integer的,也就是说Number Type就是floating point。JavaScript使用IEEE754 double precision (64-bits) floating point。
JavaScript支持一个built-in的Object为Math,使用者可以用其进行一些数学运算。

Math.sin(3.5);
const circumference = 2 * Math.PI * r;

JS支持使用parseInt来将String转换为Number。Function takes an optional second argument as the base of the string representation:

parseInt('123', 10); // 123
parseInt('010', 10); // 10
parseInt('11', 2); // 3

There is a function called parseFloat which behaves similarly with parseInt but doesn't take a second argument. Instead, it always assumes base 10 representations.
An unary + sign can be used to convert string into numbers as well:

+ '42'; // 42
+ `010`; // 10
+ `0x10`; // 16

A special value called NaN (which stands for "Not a Number") is used in JS:

parseInt('foo', 10); // NaN

Any mathematical operation operating on NaN will also return NaN (which is quite toxic...):

NaN + 5; // NaN

One can use the built-in function isNaN to test whether a value is NaN or not:

isNaN(NaN); // true

JS also supports two Infinity values:

1 / 0; Infinity
-1 / 0; -Infinity

One can also test whether a value is infinity of not by using the isFinite function:

isFinite(1 / 0); // false
isFinite(-Infinity); // false
isFinite(NaN); // false

One thing to point out for comparing the difference between + and parseInt,parseFloat is that, two parse functions will terminate once they hit invalid characters and return the value up to that point, whereas the + operand will simply return NaN if a invalid argument is given:

+ '10.2abc'; // NaN
parseInt('10.2abc', 10); // 10
parseFloat('10.2abc'); // 10.2
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,921评论 0 13
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,866评论 0 10
  • 01 圣经中有一个小故事,说以前有一个富有的财主,在出远门之前把自己的财产分成了三份,分别交给自己的三个仆人进行管...
    星晨与大海阅读 493评论 0 0
  • 她就像我小时候 学过的语文课本里的一样。 她是世上 最美的人。 喜欢她 身上岁月沉淀出来的风姿。 她有万般风情。 ...
    鱼太腥儿阅读 230评论 1 2

友情链接更多精彩内容