Just like C programming language, we can assign integer value in the different format to the variable.
就像C編程語言一樣 ,我們可以將不同格式的整數值分配給變量。
Assigning decimal value: It can be assigned simply without using any prefix.
分配十進制值:可以簡單地分配它,而無需使用任何前綴。
Assigning octal value: It can be assigned by using 0 (zero) prefix with the octal value.
分配八進制值:可以使用帶有八進制值的0 (零)前綴來分配它。
Assigning hexadecimal value: It can be assigned using 0x or 0X prefix with the hexadecimal value.
分配十六進制值:可以使用帶有十六進制值的0x或0X前綴進行分配。
Example:
例:
In the given example, we assign assigning 10 which is in decimal format to the variable a, octal format value 012 which is octal of 10 to the variable b, and hexadecimal format value 0x0A which is a hexadecimal value of 10 to the variable c, and printing all values those will be printed in decimal formats.
在給出的例子中,我們分配分配10,它是十進制格式的變量a,八進制格式值012是八進制的10到變量b,和十六進制格式值的0x0A其是10至變量c十六進制值,并打印所有將以十進制格式打印的值。
<html>
<head>
<title>JavaScipt Example</title>
</head>
<body>
<script>
var a = 10; //decimal value
var b = 012; //octal value
var c = 0x0A; //hexadecimal value
//printing values
document.write("a = " + a + "<br>");
document.write("b = " + b + "<br>");
document.write("c = " + c + "<br>");
</script>
</body>
</html>
Output
輸出量
a = 10
b = 10
c = 10
翻譯自: https://www.includehelp.com/code-snippets/assign-decimal-octal-and-hexadecimal-values-to-the-variables-javascript.aspx