JavaScript Number Object
JavaScript has only one numeric type.
You can use or write numbers without using a decimal point.
JavaScript Numbers
JavaScript numbers can be used or you can write without using a decimal point:
Instance
var pi=3.14; Use decimal point
var x=34; Do not use decimal points
The maximum or minimum number can be written by scientific (exponential) notation:
Instance
var y=123e5; 12300000
var z=123e-5; 0.00123
All JavaScript numbers are 64 bits
JavaScript is not a type language. Unlike many other programming languages, JavaScript does not define different types of numbers, such as integers, short, long, floating-point, and so on.
In JavaScript, numbers are not divided into integer types and floating-point types, and all numbers are by floating-point type. JavaScript represents a number in a 64-bit floating-point format defined by the IEEE754 standard, which indicates a maximum value of ±1.7976931348623157 x 10308 and a minimum of ±5 x 10-324
value (aka Fraction/mantissa) index Sign
Bits (0-51) bits (50-62) 1 bit (63)
Precision
Integer (no decimal point or exponential count method) is up to 15 digits.
The maximum number of decimal digits is 17, but floating-point operations are not always 100% accurate:
Instance
var x = 0.2+0.1; Result would be 0.30000000000000004
octal and hexadecimal
If the prefix is 0, JavaScript interprets numeric constants as octal numbers, and if the prefix is 0 and "X", it is interpreted as a hexadecimal number.
Instance
var y = 0377;
var z = 0xFF;
Lamp should never write 0 in front of the number unless you need to make an octal conversion.
By default, the JavaScript number is displayed in decimal.
But you can use the ToString () method to output 16, 8, 2.
Instance
var mynumber=128;
Mynumber.tostring (); Returns
mynumber.tostring (8); Returns
mynumber.tostring (2); Returns 10000000
Infinity (Infinity)
When the result of a numeric operation exceeds the upper limit (overflow) that JavaScript can represent, the result is a special infinity (infinity) value, expressed as infinity in JavaScript. Similarly, when the value of a negative number exceeds the negative range that JavaScript can represent, the result is negative infinity, expressed as-infinity in JavaScript. The behavioral properties of Infinity values are consistent with what we expect: the addition, subtraction, multiplication, and addition of their operations are infinitely large (and, of course, their positive and negative numbers).
Instance
mynumber=2;
while (mynumber!=infinity)
{
mynumber=mynumber*mynumber;//Calculate until Infinity
}
Dividing by 0 also produces infinity:
Instance
var x = 2/0;
var y =-2/0;
NaN-Non-numeric value
The NaN property is a special value that represents a Non-numeric value. This property is used to indicate 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 True
var y = m/"1000";
isNaN (y); Returns false
Divided by 0 is infinity, Infinity is a number:
Instance
var x = 1000/0;
isNaN (x); Returns false
Numbers can be numbers or objects
Numbers can be initialized with private data, just like x = 123;
JavaScript numeric object initialization data, var y = new number (123);
Instance
var x = 123;
var y = new number (123);
typeof (X)//returns number
typeof (Y)//returns Object
Instance
var x = 123;
var y = new number (123);
(x = = y)/is false because x are a number and Y is a object.
JavaScript string (String) object
A String object is used to process an existing block of characters.
JavaScript String
A string is used to store a series of characters like "John Doe".
A string can use either single or double quotes:
Instance
var carname= "Volvo XC60";
var carname= ' Volvo XC60 ';
You use the location (index) to access any character in the string:
Instance
var character=carname[7];
The index of the string starts at zero, so the first character of the string is [0], the second character is [1], and so on.
You can use quotes in a string, such as the following example:
Instance
var answer= "It ' s Alright";
var answer= "He is called ' Johnny '";
var answer= ' He is called ' Johnny ';
Or you can use the escape character in a string to enclose quotes:
Instance
var answer= ' It ' s alright ';
var answer= "He is called" Johnny "";
Strings (String)
String to compute the length of the string using length property lengths:
Instance
var txt= "Hello world!";
document.write (txt.length);
var txt= "abcdefghijklmnopqrstuvwxyz";
document.write (txt.length);
Find a string in a string
A string uses indexOf () to position the first occurrence of a specified character in a string:
Instance
var str= "Hello World, Welcome to the universe";
var n=str.indexof ("Welcome");
If no corresponding character function is found, return-1
The LastIndexOf () method begins at the end of the string to find where the string appears.
Content Matching
The match () function is used to find a specific character in a string and, if found, returns the character.
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 characters with some character in a string.
Instance
Str= "Please visit microsoft!"
var n=str.replace ("Microsoft", "W3cschool");
String Capitalization conversions
string capitalization conversion using Functions toUpperCase ()/toLowerCase ():
Instance
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
String to Array
strings are converted to arrays using the Strong>split () function:
Instance
txt= "A,b,c,d,e" //String
txt.split (","); Split on commas
txt.split (""); Split on Spaces
Txt.split ("|");
Special characters
You can use a backslash (\) in Javascript to insert special symbols, such as apostrophes, quotes, and other special symbols.
View the following JavaScript code:
var txt= "We are the so-called" Vikings "from";
document.write (TXT);
In JavaScript, the start and stop of a string uses either single or double quotes. This means that the above string will be cut into: We are the so-called
To resolve the above problem, you can use a backslash to escape the quotation marks:
var txt= "We are the so-called \" Vikings\ "from";
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 that you can use to escape special characters: