ๆญค้ ้ข็”ฑ็คพ็พคๅพž่‹ฑๆ–‡็ฟป่ญฏ่€Œไพ†ใ€‚ไบ†่งฃๆ›ดๅคšไธฆๅŠ ๅ…ฅ MDN Web Docs ็คพ็พคใ€‚

View in English Always switch to English

ๆ•ธๅญ—่ˆ‡ๆ—ฅๆœŸ

้€™ๅ€‹็ซ ็ฏ€ๅฐ‡ไป‹็ดนๅฆ‚ไฝ•ๅœจ JavaScript ไธญ่™•็†ๆ•ธๅญ—่ˆ‡ๆ—ฅๆœŸใ€‚

ๆ•ธๅญ—(Numbers)

ๅœจ JavaScript ไธญ, Number ๆ‰€ไฝฟ็”จ็š„ๆจ™ๆบ–ไพ็…ง double-precision 64-bit binary format IEEE 754 (i.e. number ็š„ๅ€้–“ๆ˜ฏ -(2^53 -1) ๅˆฐ 2^53 -1)ใ€‚ๆ•ดๆ•ธๆ˜ฏๆฒ’ๆœ‰็‰นๅฎš็š„้กžๅž‹ใ€‚

ๆญคๅค–้‚„ๅฏไปฅ้กฏ็คบๆตฎ้ปžๆ•ธ๏ผŒไธ‰็จฎ็ฌฆ่™Ÿๆ•ธๅ€ผ: +Infinity๏ผŒ -Infinity๏ผŒ and NaN (not-a-number)ใ€‚

BigInt ๆ˜ฏ Javascript ๆœ€ๆ–ฐ็š„ๅŠŸ่ƒฝ๏ผŒๅฎƒๅฏไปฅ่กจ็คบไธ€ๅ€‹ๅพˆๅคง็š„ๆ•ดๆ•ธใ€‚ไฝฟ็”จ BigInt้œ€่ฆๆณจๆ„ไธ€้ปž๏ผŒBigInt ๅ’ŒNumberไธ่ƒฝๅœจๅŒไธ€ๅ€‹ operation ๆทท็”จ้‚„ๆœ‰็•ถ็”จ Math ็‰ฉไปถๆ™‚ไธ่ƒฝไฝฟ็”จBigIntใ€‚

่ซ‹ๅƒ็…ง JavaScript data types and structures ไพ†ๅ–ๅพ—ๆ›ดๅคš่ฉณ็ดฐ่ณ‡ๆ–™ใ€‚

ไฝ ๅฏไปฅ็”จๅ››็จฎ้€ฒๅˆถ่กจ็คบๆ•ธๅญ—๏ผšๅ้€ฒๅˆถ (decimal)๏ผŒไบŒ้€ฒๅˆถ (binary)๏ผŒๅ…ซ้€ฒๅˆถ (octal) ไปฅๅŠๅๅ…ญ้€ฒๅˆถ (hexadecimal)ใ€‚

ๅ้€ฒๅˆถๆ•ธๅ€ผ

js
1234567890;
42;

// ไปฅ้›ถ็‚บ้–‹้ ญๆ™‚่ฆๅฐๅฟƒ๏ผš

0888; // 888 ่งฃๆž็‚บ ๅ้€ฒๅˆถๆ•ธๅ€ผ
0777; // ๅœจ non-strict ๆจกๅผไธ‹ๅฐ‡่งฃๆžๆˆๅ…ซ้€ฒๅˆถ (็ญ‰ๅŒๆ–ผๅ้€ฒๅˆถ็š„ 511)

่ซ‹ๆณจๆ„๏ผŒๅ้€ฒไฝๆ•ธๅญ—ๅ…่จฑ็ฌฌไธ€ๅ€‹ๆ•ธๅญ—่จญ็‚บ้›ถ(0)็š„่ฉฑ๏ผŒๅ‰ๆๆ˜ฏๅพŒ้ขๆŽฅ็š„ๆ•ธๅญ—ๅฟ…้ ˆ่ฆๆœ‰ไธ€ๅ€‹ๆ•ธๅญ—ๅคงๆ–ผ 8(ไพ‹ๅฆ‚่ผธๅ…ฅ 0888 ็ตๆžœๆœƒๆ˜ฏ 888๏ผŒ่ผธๅ…ฅ 068 ็ตๆžœๆœƒๆ˜ฏ 68)๏ผŒไธ็„ถๅ‰‡ๆœƒ่ขซ่ฝ‰ๆˆ๏ผ˜้€ฒไฝ(ไพ‹ๅฆ‚ 0777 ็ตๆžœๆœƒๆ˜ฏ 511๏ผŒ่ผธๅ…ฅ 063 ็ตๆžœๆœƒๆ˜ฏ 51)ใ€‚

ไบŒ้€ฒๅˆถๆ•ธๅ€ผ

ไบŒ้€ฒๅˆถๆ•ธๅ€ผไปฅ 0 ็‚บ้–‹้ ญไธฆ่ทŸ่‘—ไธ€ๅ€‹ๅคงๅฏซๆˆ–ๅฐๅฏซ็š„่‹ฑๆ–‡ๅญ—ๆฏ ใ€ŒBใ€ (0b ๆˆ– 0B)ใ€‚ๅฆ‚ๆžœ 0b ๅพŒ้ขๆŽฅ่‘—็š„ๆ•ธๅญ—ไธๆ˜ฏ 0 ๆˆ– 1๏ผŒ้‚ฃๆœƒไธŸๅ‡บ SyntaxError(่ชžๆณ•้Œฏ่ชค): "Missing binary digits after 0b"ใ€‚

js
var FLT_SIGNBIT = 0b10000000000000000000000000000000; // 2147483648
var FLT_EXPONENT = 0b01111111100000000000000000000000; // 2139095040
var FLT_MANTISSA = 0b00000000011111111111111111111111; // 8388607

ๅ…ซ้€ฒๅˆถๆ•ธๅ€ผ

ๅ…ซ้€ฒๅˆถๆ•ธๅ€ผไปฅ 0 ็‚บ้–‹้ ญใ€‚ๅฆ‚ๆžœ 0 ๅพŒ้ข็š„ๆ•ธๅญ—่ถ…ๅ‡บ 0 ๅˆฐ 7 ้€™ๅ€‹็ฏ„ๅœ๏ผŒๅฐ‡ๆœƒ่ขซ่งฃๆžๆˆๅ้€ฒๅˆถๆ•ธๅ€ผใ€‚

js
var n = 0755; // 493
var m = 0644; // 420

Strict mode in ECMAScript 5 forbids octal syntax. Octal syntax isn't part of ECMAScript 5, but it's supported in all browsers by prefixing the octal number with a zero: 0644 === 420 and"\045" === "%". In ECMAScript 2015, octal numbers are supported if they are prefixed with 0o, e.g.:

js
var a = 0o10; // ES2015: 8

ๅๅ…ญ้€ฒๅˆถๆ•ธๅ€ผ

ๅๅ…ญ้€ฒๅˆถๆ•ธๅ€ผไปฅ 0 ็‚บ้–‹้ ญไธฆ่ทŸ่‘—ไธ€ๅ€‹ๅคงๅฏซๆˆ–ๅฐๅฏซ็š„่‹ฑๆ–‡ๅญ—ๆฏ ใ€ŒXใ€(0x ๆˆ– 0X)ใ€‚ๅฆ‚ๆžœ 0b ๅพŒ้ขๆŽฅ่‘—็š„ๅ€ผ่ถ…ๅ‡บ็ฏ„ๅœ (0123456789ABCDEF)๏ผŒ้‚ฃๆœƒไธŸๅ‡บ SyntaxError(่ชžๆณ•้Œฏ่ชค):"Identifier starts immediately after numeric literal"ใ€‚

js
0xfffffffffffffffff; // 295147905179352830000
0x123456789abcdef; // 81985529216486900
0xa; // 10

ๆŒ‡ๆ•ธ้‹็ฎ—

js
1e3; // 1000
2e6; // 2000000
0.1e2; // 10

Number ็‰ฉไปถ

The built-in Number object has properties for numerical constants, such as maximum value, not-a-number, and infinity. You cannot change the values of these properties and you use them as follows:

js
var biggestNum = Number.MAX_VALUE;
var smallestNum = Number.MIN_VALUE;
var infiniteNum = Number.POSITIVE_INFINITY;
var negInfiniteNum = Number.NEGATIVE_INFINITY;
var notANum = Number.NaN;

You always refer to a property of the predefined Number object as shown above, and not as a property of a Number object you create yourself.

ไธ‹้ข้€™ๅผต่กจๆ ผๆ•ด็†ไบ† Number ็‰ฉไปถ็š„ๅฑฌๆ€ง

Number ็š„ๅฑฌๆ€ง

ๅฑฌๆ€ง ๆ่ฟฐ
Number.MAX_VALUE ๅฏ่กจ็คบ็š„ๆœ€ๅคงๆ•ธๅ€ผ
Number.MIN_VALUE ๅฏ่กจ็คบ็š„ๆœ€ๅฐๆ•ธๅ€ผ
Number.NaN ่กจ็คบใ€Œ้žๆ•ธๅ€ผใ€๏ผˆNot-A-Number๏ผ‰็š„ๆ•ธๅ€ผ
Number.NEGATIVE_INFINITY Special negative infinite value; returned on overflow
Number.POSITIVE_INFINITY Special positive infinite value; returned on overflow
Number.EPSILON Difference between one and the smallest value greater than one that can be represented as a Number.
Number.MIN_SAFE_INTEGER ๅฏไปฅๅœจ JavaScript ไธญๅฎ‰ๅ…จ่กจ็คบ็š„ๆœ€ๅฐๆ•ธๅ€ผใ€‚
Number.MAX_SAFE_INTEGER ๅฏไปฅๅœจ JavaScript ไธญๅฎ‰ๅ…จ่กจ็คบ็š„ๆœ€ๅคงๆ•ธๅ€ผใ€‚
ๆ–นๆณ• ๆ่ฟฐ
Number.parseFloat() ๅญ—ไธฒ่ฝ‰ๆ›ๆˆๆตฎ้ปžๆ•ธใ€‚ ็ญ‰ๅŒๆ–ผๅ…จๅŸŸๅ‡ฝๅผ parseFloat() ใ€‚
Number.parseInt() ไปฅๆŒ‡ๅฎš็š„ๅŸบๆ•ธๅฐ‡ๅญ—ไธฒ่ฝ‰ๆ›ๆˆๆ•ดๆ•ธใ€‚ ็ญ‰ๅŒๆ–ผๅ…จๅŸŸๅ‡ฝๅผ parseInt() ใ€‚
Number.isFinite() ๅˆคๅฎš็ตฆๅฎš็š„ๅ€ผๆ˜ฏไธๆ˜ฏไธ€ๅ€‹ๆœ‰้™ๆ•ธใ€‚
Number.isInteger() ๅˆคๅฎš็ตฆๅฎš็š„ๅ€ผๆ˜ฏไธๆ˜ฏไธ€ๅ€‹ๆ•ดๆ•ธ
Number.isNaN() Determines whether the passed value is NaN. More robust version of the original global isNaN().
Number.isSafeInteger() Determines whether the provided value is a number that is a safe integer.

The Number prototype provides methods for retrieving information from Number objects in various formats. The following table summarizes the methods of Number.prototype.

ๆ–นๆณ• ๆ่ฟฐ
toExponential() Returns a string representing the number in exponential notation.
toFixed() Returns a string representing the number in fixed-point notation.
toPrecision() Returns a string representing the number to a specified precision in fixed-point notation.

Math ็‰ฉไปถ

The built-in Math object has properties and methods for mathematical constants and functions. For example, the Math object's PI property has the value of pi (3.141...), which you would use in an application as

js
Math.PI;

Similarly, standard mathematical functions are methods of Math. These include trigonometric, logarithmic, exponential, and other functions. For example, if you want to use the trigonometric function sine, you would write

js
Math.sin(1.56);

Note that all trigonometric methods of Math take arguments in radians.

The following table summarizes the Math object's methods.

ๆ–นๆณ• ๆ่ฟฐ
abs() ็ต•ๅฐๅ€ผ
sin(), cos(), tan() ไธ‰่ง’ๅ‡ฝๆ•ธ; ๅผ•ๆ•ธไปฅๅผณๅบฆ่กจ็คบ
asin(), acos(), atan(), atan2() ๅไธ‰่ง’ๅ‡ฝๆ•ธ; ๅ›žๅ‚ณๅ€ผไปฅๅผณๅบฆ่กจ็คบ
sinh(), cosh(), tanh() ้›™ๆ›ฒๅ‡ฝๆ•ธ; ๅผ•ๆ•ธไปฅ hyperbolic angle ่กจ็คบ
asinh(), acosh(), atanh() ๅ้›™ๆ›ฒๅ‡ฝๆ•ธ; ๅ›žๅ‚ณๅ€ผไปฅ hyperbolic angle ่กจ็คบ
pow(), exp(), expm1(), log10(), log1p(), log2() ๆŒ‡ๆ•ธๅŠๅฐๆ•ธๅ‡ฝๅผ
floor(), ceil() ๅ›žๅ‚ณๅฐๆ–ผ็ญ‰ๆ–ผ/ๅคงๆ–ผ็ญ‰ๆ–ผๆŒ‡ๅฎšๆ•ธๅญ—็š„ๆœ€ๅคง/ๆœ€ๅฐๆ•ดๆ•ธ
min(), max() Returns lesser or greater (respectively) of comma separated list of numbers arguments
random() ๅ›žๅ‚ณไธ€ๅ€‹ไป‹ๆ–ผ 0 ๅˆฐ 1 ไน‹้–“็š„ๆ•ธๅ€ผ
round(), fround(), trunc(), Rounding and truncation functions.
sqrt(), cbrt(), hypot() Square root, cube root, Square root of the sum of square arguments.
sign() The sign of a number, indicating whether the number is positive, negative or zero.
clz32(), imul() Number of leading zero bits in the 32-bit binary representation. The result of the C-like 32-bit multiplication of the two arguments.

Unlike many other objects, you never create a Math object of your own. You always use the built-in Math object.

Date ็‰ฉไปถ

JavaScript ๆฒ’ๆœ‰ๆ‰€่ฌ‚ๆ—ฅๆœŸ(date)็š„ๆ•ธๆ“šๅž‹ๆ…‹(data type)ใ€‚ไฝ ๅฏไปฅไฝฟ็”จ Date ็‰ฉไปถๅŠๅ…ถๆ–นๆณ•ๅŽป่จญๅฎšๆ—ฅๆœŸ่ทŸๆ™‚้–“ไพ†ๆปฟ่ถณไฝ ็š„้œ€ๆฑ‚ ใ€‚Date ็‰ฉไปถๆœ‰ๅคง้‡็š„่จญๅฎšๅ–ๅพ—ๆ“ไฝœๆ—ฅๆœŸ็š„ๆ–นๆณ•(method)๏ผŒไฝ†ๅฎƒๆฒ’ๆœ‰ๅฑฌๆ€งใ€‚

JavaScript ่™•็†ๆ—ฅๆœŸ็š„ๆ–นๅผ่ทŸ Java ้กžไผผใ€‚้€™ๅ…ฉๅ€‹่ชž่จ€ๆœ‰่จฑๅคšไธ€ๆจฃ็š„ date ๆ–นๆณ•๏ผŒไธ”้ƒฝๅฐ‡ๆ—ฅๆœŸๅ„ฒๅญ˜็‚บๅพž 1970 ๅนด 1 ๆœˆ 1 ่™Ÿ 0 ๆ™‚ 0 ๅˆ† 0 ็ง’ไปฅไพ†็š„ๆฏซ็ง’ๆ•ธ(millisecond)ใ€‚

Date ็‰ฉไปถ็ฏ„ๅœๆ˜ฏ -100,000,000 days to 100,000,000 days ไปฅ 1970 ๅนด 1 ๆœˆ 1 ่™Ÿ 0 ๆ™‚ 0 ๅˆ† 0 ็ง’ UTC ็‚บๅŸบๆบ–ใ€‚

ๅ‰ตๅปบไธ€ๅ€‹Date็‰ฉไปถ:

js
var dateObjectName = new Date([parameters]);

ๅœจ้€™่ฃกๅ‰ตๅปบไธ€ๅ€‹ๅ็‚บdateObjectName ็š„ Date ็‰ฉไปถ๏ผ›ๅฎƒๅฏไปฅๆ˜ฏไธ€ๅ€‹ๆ–ฐ็š„็‰ฉไปถๆˆ–ๆ˜ฏๆŸๅ€‹ไปฅๅญ˜ๅœจ็š„็‰ฉไปถ็•ถไธญ็š„ๅฑฌๆ€งใ€‚

Calling Date without the new keyword returns a string representing the current date and time.

The parameters in the preceding syntax can be any of the following:

  • Nothing: creates today's date and time. For example, today = new Date();.
  • A string representing a date in the following form: "Month day, year hours:minutes:seconds." For example, var Xmas95 = new Date("December 25, 1995 13:30:00"). If you omit hours, minutes, or seconds, the value will be set to zero.
  • A set of integer values for year, month, and day. For example, var Xmas95 = new Date(1995, 11, 25).
  • A set of integer values for year, month, day, hour, minute, and seconds. For example, var Xmas95 = new Date(1995, 11, 25, 9, 30, 0);.

Date ็š„ๆ–นๆณ•

The Date object methods for handling dates and times fall into these broad categories:

  • "set" methods, for setting date and time values in Date objects.
  • "get" methods, for getting date and time values from Date objects.
  • "to" methods, for returning string values from Date objects.
  • parse and UTC methods, for parsing Date strings.

With the "get" and "set" methods you can get and set seconds, minutes, hours, day of the month, day of the week, months, and years separately. There is a getDay method that returns the day of the week, but no corresponding setDay method, because the day of the week is set automatically. These methods use integers to represent these values as follows:

  • Seconds and minutes: 0 ๅˆฐ 59
  • Hours: 0 ๅˆฐ 23
  • Day: 0 (ๆ˜ŸๆœŸๆ—ฅ) ๅˆฐ 6 (ๆ˜ŸๆœŸๅ…ญ)
  • Date: 1 ๅˆฐ 31 (้€™ๅ€‹ๆœˆ็š„็ฌฌๅนพๅคฉ)
  • Months: 0 (ไธ€ๆœˆ) ๅˆฐ 11 (ๅไบŒๆœˆ)
  • Year: years since 1900

่ˆ‰ไพ‹๏ผŒๅ‡่จญไฝ ๅฎš็พฉไบ†ไธ€ๅ€‹ๆ—ฅๆœŸๅฆ‚ไธ‹๏ผš

js
var Xmas95 = new Date("December 25, 1995");

้‚ฃ Xmas95.getMonth() ๅฐ‡ๆœƒๅ›žๅ‚ณ 11๏ผŒ Xmas95.getFullYear() ๆœƒๅ›žๅ‚ณ 1995ใ€‚

getTime ๅŠ setTime ้€™ๅ…ฉๅ€‹ๆ–นๆณ•ๅฐๆ–ผๆฏ”่ผƒๆ—ฅๆœŸๆœ‰ๅนซๅŠฉใ€‚ The getTime method returns the number of milliseconds since January 1, 1970, 00:00:00 for a Date object.

For example, the following code displays the number of days left in the current year:

js
var today = new Date();
var endYear = new Date(1995, 11, 31, 23, 59, 59, 999); // Set day and month
endYear.setFullYear(today.getFullYear()); // Set year to this year
var msPerDay = 24 * 60 * 60 * 1000; // Number of milliseconds per day
var daysLeft = (endYear.getTime() - today.getTime()) / msPerDay;
var daysLeft = Math.round(daysLeft); //returns days left in the year

This example creates a Date object named today that contains today's date. It then creates a Date object named endYear and sets the year to the current year. Then, using the number of milliseconds per day, it computes the number of days between today and endYear, using getTime and rounding to a whole number of days.

The parse method is useful for assigning values from date strings to existing Date objects. For example, the following code uses parse and setTime to assign a date value to the IPOdate object:

js
var IPOdate = new Date();
IPOdate.setTime(Date.parse("Aug 9, 1995"));

็ฏ„ไพ‹

ไธ‹้ข้€™ๅ€‹็ฏ„ไพ‹๏ผŒJSClock() ้€™ๅ€‹ๅ‡ฝๅผๅฐ‡ๆœƒไปฅๆ•ธไฝๆ™‚้˜็š„ๆ ผๅผๅ›žๅ‚ณๆ™‚้–“ใ€‚

js
function JSClock() {
  var time = new Date();
  var hour = time.getHours();
  var minute = time.getMinutes();
  var second = time.getSeconds();
  var temp = "" + (hour > 12 ? hour - 12 : hour);
  if (hour == 0) temp = "12";
  temp += (minute < 10 ? ":0" : ":") + minute;
  temp += (second < 10 ? ":0" : ":") + second;
  temp += hour >= 12 ? " P.M." : " A.M.";
  return temp;
}

JSClock ้€™ๅ€‹ๅ‡ฝๅผๆœƒๅ…ˆๅปบ็ซ‹ไธ€ๅ€‹ๅ็‚บ time ็š„ Date ็‰ฉไปถ; ๅ› ็‚บๆฒ’ๆœ‰ๆไพ›ไปปไฝ•ๅผ•ๆ•ธ๏ผŒๆ‰€ไปฅๆœƒๆ”พๅ…ฅ็›ฎๅ‰็š„ๆ—ฅๆœŸๅŠๆ™‚้–“ใ€‚ๆŽฅ่‘—ๅ‘ผๅซ getHours ใ€ getMinutes ไปฅๅŠ getSeconds ้€™ไธ‰ๅ€‹ๆ–นๆณ•ๅฐ‡็พๅœจ็š„ๆ™‚ใ€ๅˆ†ไปฅๅŠ็ง’ๅˆ†ๅˆฅๆŒ‡ๅฎš็ตฆ hour ใ€ minute ไปฅๅŠ second ้€™ไธ‰ๅ€‹่ฎŠๆ•ธใ€‚

ๆŽฅ่‘—็š„ๅ››่กŒๆŒ‡ไปคๅฐ‡ๆœƒๅปบ็ซ‹ไธ€ๅ€‹ๆ™‚้–“็š„ๅญ—ไธฒใ€‚็ฌฌไธ€่กŒ็š„ๆŒ‡ไปคๅปบ็ซ‹ไบ†ไธ€ๅ€‹่ฎŠๆ•ธ temp๏ผŒไปฅๆขไปถ้‹็ฎ—ๅผๆŒ‡ๅฎšๅ€ผ; ๅฆ‚ๆžœ hour ๅคงๆ–ผ 12๏ผŒ้‚ฃๅฐฑๆŒ‡ๅฎš (hour - 12)๏ผŒไธ็„ถๆœƒ็›ดๆŽฅๆŒ‡ๅฎš hour๏ผŒ ไฝ†ๅฆ‚ๆžœ hour ็ญ‰ๆ–ผ 0 ๏ผŒ ๅ‰‡ๆ”น็‚บ 12ใ€‚

ๆŽฅ่‘—ไธ‹ไธ€่กŒๅฐ‡ minute ๅŠ ๅˆฐ temp ไธญใ€‚ๅฆ‚ๆžœ minute ๅฐๆ–ผ 10, ๅ‰‡ๆœƒๅœจ้™„ๅŠ ๆ™‚่ฃœไธŠไธ€ๅ€‹้›ถ; ไธ็„ถ็š„่ฉฑๆœƒ็›ดๆŽฅๅŠ ไธŠๅ†’่™ŸๅŠๅˆ†้˜ๆ•ธใ€‚็ง’ๆ•ธไนŸๆ˜ฏไปฅๅŒๆจฃ็š„ไฝœๆณ•้™„ๅŠ ๅˆฐ temp ไธŠใ€‚

ๆœ€ๅพŒ๏ผŒๅˆคๆ–ท hour ๆ˜ฏไธๆ˜ฏๅคงๆ–ผ็ญ‰ๆ–ผ 12 ๏ผŒๅฆ‚ๆžœๆ˜ฏๅฐฑๅœจ temp ๅŠ ไธŠ "P.M." ๏ผŒไธ็„ถๅฐฑๅŠ ไธŠ "A.M."ใ€‚