Home  >  Article  >  Web Front-end  >  In-depth analysis of numeric objects and string objects in JavaScript_Basic knowledge

In-depth analysis of numeric objects and string objects in JavaScript_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 15:35:441167browse

JavaScript Number object
JavaScript has only one number type.
Numbers can be written with or without a decimal point.
JavaScript Numbers
JavaScript numbers can be written with or without a decimal point:
Example

var pi=3.14;  // 使用小数点
var x=34;    // 不使用小数点


Very large or very small numbers can be written using scientific (exponential) notation:
Example

var y=123e5;  // 12300000
var z=123e-5;  // 0.00123

All JavaScript numbers are 64-bit
JavaScript is not a typed language. Unlike many other programming languages, JavaScript does not define different types of numbers, such as integer, short, long, floating point, etc.
In JavaScript, numbers are not divided into integer types and floating-point types. All numbers are of floating-point type. JavaScript uses the 64-bit floating point format defined by the IEEE754 standard to represent numbers. It can represent a maximum value of ±1.7976931348623157 x 10308 and a minimum value of ±5 x 10 -324
Value (aka Fraction/Mantissa) Index Sign

52 bits (0 - 51) 11 bits (50 - 62) 1 bit (63)

Accuracy
Integers (without decimal point or exponent notation) can be up to 15 digits.
The maximum number of decimal places is 17, but floating point arithmetic is not always 100% accurate:
Example

var x = 0.2+0.1; // result will be 0.30000000000000004


Octal and Hexadecimal
JavaScript interprets numeric constants as octal if the prefix is ​​0, and as hexadecimal if the prefix is ​​0 and "x".
Example

var y = 0377; 
var z = 0xFF;


lamp Never write zeros in front of a number unless you need to do an octal conversion.
By default, JavaScript numbers are displayed in decimal notation.
But you can use the toString() method to output hexadecimal, octal, or binary.
Example

var myNumber=128;
myNumber.toString(16);  // returns 80
myNumber.toString(8);  // returns 200
myNumber.toString(2);  // returns 10000000


Infinity
When the result of a numerical operation exceeds the upper limit of the number that JavaScript can represent (overflow), the result is a special infinity value, which is represented by Infinity in JavaScript. Similarly, when the value of a negative number exceeds the range of negative numbers that JavaScript can represent, the result is negative infinity, which is represented by -Infinity in JavaScript. Infinite values ​​behave as we would expect: operations based on their addition, subtraction, multiplication, and division still result in infinity (while retaining their signs, of course).
Example

myNumber=2;
while (myNumber!=Infinity)
{
myNumber=myNumber*myNumber; // Calculate until Infinity
}

Dividing by 0 also produces infinity:
Example

var x = 2/0;
var y = -2/0;


NaN - non-numeric value
The NaN attribute is a special value that represents a non-numeric value. This attribute is used to indicate that a value is not a number. A Number object can be set to this value to indicate that it is not a numeric value.
You can use the isNaN() global function to determine whether a value is a NaN value.
Example

var x = 1000 / "Apple";
isNaN(x); // returns true
var y = 100 / "1000";
isNaN(y); // returns false

Dividing by 0 is infinity, which is a number:
Example

var x = 1000 / 0;
isNaN(x); // returns false


Numbers can be numbers or objects
Numbers can be initialized with private data, like x = 123;
JavaScript number object initialization data, var y = new Number(123);
Example

var x = 123;
var y = new Number(123);
typeof(x) // returns Number
typeof(y) // returns Object

Example

var x = 123;       
var y = new Number(123);
(x === y) // is false because x is a number and y is an object.

JavaScript String Object
String objects are used to process existing blocks of characters.
JavaScript String
A string is used to store a sequence of characters like "John Doe".
A string can use single or double quotes:
Example

var carname="Volvo XC60";
var carname='Volvo XC60';


You can access any character in the string using position (index):
Example

var character=carname[7];


String indexing starts at zero, so the first character in the string is [0], the second character is [1], and so on.
You can use quotes in strings, as in the following example:
Example

var answer="It's alright";
var answer="He is called 'Johnny'";
var answer='He is called "Johnny"';


Or you can use quotes within the string using escape characters:
Example

var answer='It's alright';
var answer="He is called "Johnny"";

String
String uses the length attribute to calculate the length of the string:
Example

var txt="Hello World!";
document.write(txt.length);

var txt="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
document.write(txt.length);

Find string within string
Strings use indexOf() to locate the first occurrence of a specified character in the string:
Example

var str="Hello world, welcome to the universe.";
var n=str.indexOf("welcome");

If the corresponding character function is not found, it returns -1
The lastIndexOf() method searches for occurrences of a string starting at the end of the string.
Content matching
The match() function is used to find a specific character in a string and, if found, returns the character.
Example

var str="Hello world!";
document.write(str.match("world") + "<br>");
document.write(str.match("World") + "<br>");
document.write(str.match("world!"));

替换内容
replace() 方法在字符串中用某些字符替换另一些字符。
实例

str="Please visit Microsoft!"
var n=str.replace("Microsoft","w3cschool");

字符串大小写转换
字符串大小写转换使用函数 toUpperCase() / toLowerCase():
实例

var txt="Hello World!";    // String
var txt1=txt.toUpperCase();  // txt1 is txt converted to upper
var txt2=txt.toLowerCase();  // txt2 is txt converted to lower

字符串转为数组
字符串使用strong>split()函数转为数组:
实例

txt="a,b,c,d,e"  // String
txt.split(",");  // Split on commas
txt.split(" ");  // Split on spaces
txt.split("|");  // Split on pipe 

特殊字符
Javascript 中可以使用反斜线()插入特殊符号,如:撇号,引号等其他特殊符号。
查看如下 JavaScript 代码:

var txt="We are the so-called "Vikings" from the north.";
document.write(txt);


在JavaScript中,字符串的开始和停止使用单引号或双引号。这意味着,上面的字符串将被切成: We are the so-called
解决以上的问题可以使用反斜线来转义引号:

var txt="We are the so-called \"Vikings\" from the north.";
document.write(txt);


JavaScript将输出正确的文本字符串:We are the so-called "Vikings" from the north.
下表列出其他特殊字符,可以使用反斜线转义特殊字符:
In-depth analysis of numeric objects and string objects in JavaScript_Basic knowledge

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn