In-depth parsing of JavaScript numeric and string objects, javascript strings

Source: Internet
Author: User

In-depth parsing of JavaScript numeric and string objects, javascript strings

JavaScript Number object
JavaScript has only one numeric type.
You can use or do not use the decimal point to write numbers.
JavaScript numbers
JavaScript numbers can be written using or without decimal points:
Instance

Var pi = 3.14; // use the decimal point var x = 34; // do not use the decimal point


Numbers that are extremely large or extremely small can be written by scientific (exponential) Notation:
Instance

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

All JavaScript numbers are 64-bit
JavaScript is not a type language. Unlike many other programming languages, JavaScript does not define different types of numbers, such as integers, short, long, and floating points.
In JavaScript, numbers are not classified into integer and floating-point types. All numbers are of the floating-point type. JavaScript uses the 64-bit floating point format defined by IEEE754 to represent a number. It can represent a maximum value of ± 1. 7976931348623157x10308, and a minimum value of ± 5x10-324.
Value (aka Fraction/Mantissa) index Sign

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

Precision
An integer (without the decimal point or exponential Notation) can contain up to 15 digits.
The maximum number of decimal places is 17, but the floating point operation is not always 100% accurate:
Instance

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


Octal and hexadecimal
If the prefix is 0, JavaScript interprets the numeric constant as the octal number. If the prefix is 0 and "x", it is interpreted as the hexadecimal number.
Instance

var y = 0377; var z = 0xFF;


Lamp never writes zero before a number unless you need to perform an octal conversion.
By default, JavaScript numbers are displayed in decimal format.
However, you can use the toString () method to output hexadecimal, octal, and binary.
Instance

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


Infinity)
When the number calculation result exceeds the upper limit (overflow) That JavaScript can represent, the result is a special infinity value, which is expressed in Infinity in JavaScript. Similarly, when the value of a negative number exceeds the range of a negative number expressed by JavaScript, the result is negative Infinity, which is represented by-Infinity in JavaScript. The behavior characteristics of the infinite values are consistent with what we expect: Based on their addition, subtraction, multiplication, and division, the results are still infinite (of course, their plus and minus signs are also retained ).
Instance

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

Dividing by 0 also produces an infinite number:
Instance

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 indicates that a value is not a number. You can set the Number object 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.
Instance

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

Dividing by 0 is an infinity, and an infinity is a number:
Instance

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


A number can be a number or an object.
Numbers can be initialized with private data, just as x = 123;
JavaScript numeric object initialization data, var y = new Number (123 );
Instance

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

Instance

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
The String object is used to process existing character blocks.
JavaScript string
A string is used to store a series of characters like "John Doe ".
A string can use single or double quotation marks:
Instance

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


You can use location (INDEX) to access any character in the string:
Instance

var character=carname[7];


The index of a string starts from zero, so the first character of the string is [0], the second character is [1], and so on.
You can use quotation marks in a string, as shown in the following example:
Instance

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


Alternatively, you can use the following quotation marks for escape characters in a string:
Instance

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

String)
String uses the length attribute to calculate the length of a String:
Instance

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

Search for strings in strings
The string uses indexOf () to locate the first occurrence of a specified character in the string:
Instance

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

If the corresponding character function is not found,-1 is returned.
The lastIndexOf () method starts searching for the position where the string appears at the end of the string.
Content matching
The match () function is used to find a specific character in a string. If yes, this character is returned.
Instance

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

Replace content
The replace () method replaces some other characters with some characters in the string.
Instance

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

String case-sensitive Conversion
Use the toUpperCase ()/toLowerCase () function for string case-insensitive conversion ():
Instance

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

Convert string to array
The string is converted into an array using the strong> split () function:
Instance

txt="a,b,c,d,e"  // Stringtxt.split(",");  // Split on commastxt.split(" ");  // Split on spacestxt.split("|");  // Split on pipe 

Special characters
In Javascript, you can use backslash (\) to insert special symbols, such as the apostrophes and quotation marks.
View the following JavaScript code:

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


In JavaScript, the start and end of a string use single or double quotation marks. This means that the above string will be cut into: We are the so-called
To solve the preceding problem, use the backslash to escape quotation marks:

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


JavaScript will output the correct text string: We are the so-called "Vikings" from the north.
The following table lists other special characters. You can use the backslash to escape special characters:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.