This article describes the common built-in functions usage in JavaScript. Share it for your reference, as follows:
1. Introduction
When using the JavaScript language, in addition to customizing functions, you can also use JavaScript built-in functions, which are functions provided by the JavaScript language itself.
2. Some commonly used built-in functions are introduced in detail
1. parseInt() function
This function mainly converts a string with the first digit into a number. If the string does not start with a number, NaN will be returned.
grammar:
parseInt(StringNum,[n])
StringNum: A string that needs to be converted to an integer.
n: Provides a number between 2 and 36 to represent the number of stored numbers. This parameter is not required in the function.
2. parseFloat() function
This function mainly converts a string with the first digit into a floating point number. If the string does not start with a digit, NaN will be returned.
grammar:
parseFloat(StringNum)
StringNum: A string that needs to be converted to a floating point type.
3. isNaN() function
This function is mainly used to check whether a value is NaN.
grammar:
isNaN(Num)
Num: Numbers that need to be verified.
Note: If the parameter Num is NaN, the function returns true; if the parameter Num is not NaN, the function returns false.
4. isFinite() function
This function is mainly used to check whether an expression is infinity.
grammar:
isFinite(Num)
Num: Numbers that need to be verified.
Note: If the parameter Num is infinity, the function returns true; if the parameter Num is not infinity, the function returns false.
5. encodeURI() function
This function is mainly used to return the result after encoding a URI string.
grammar:
encodeURI(url)
url: A string that needs to be converted into a network resource address.
Note: Both URI and URL can represent network resource addresses. URIs are more extensive than URLs, but in general, URIs and URLs can be equivalent.encodeURI()
Functions escape only meaningful characters in strings. For example, convert spaces in strings to "%20".
6. DecodeURI() function
This function is mainly used to decode strings encoded as URIs into the initial string and return them.
grammar:
decodeURI(url)
url: The network resource address that needs to be decoded.
Note: This function can be usedencodeURI()
The transcoded network resource address is converted into a string and returned, that is,decodeURI()
The function isencodeURI()
Reverse operation of the function.
3. Code
<script type="text/javascript"> /* parseInt() function */ var num1="123abc" var num2="abc123" ("(1)useparseInt()function:<br>"); ("123abc conversion result is:"+parseInt(num1)+"<br>"); ("abc123 conversion result is:"+parseInt(num2)+"<br><br>"); /* parseFloat() function */ var num3="123.456789abc" ("(2)useparseFloat()function:<br>"); ("123.456789abc conversion result is:"+parseFloat(num3)+"<br><br>"); /* isNaN() function */ ("(3)useisNaN()function:<br>"); ("123.456789Abc is NaN after conversion:"+isNaN(parseFloat(num3))+"<br>"); ("Is the abc123 conversion result NaN:"+isNaN(parseInt(num2))+"<br><br>"); /* isFinite() function */ ("(4)useisFinite()function<br>"); ("Is the result of dividing 1 by 0 infinite?"+isFinite(1/0)+"<br><br>"); /* encodeURI() function */ ("(5)useencodeURI()function<br>"); ("Convert to network resource address as:"+encodeURI("http://127.0.0.1/?name=test")+"<br><br>");/* decodeURI() function */ ("(6)usedecodeURI()function<br>"); ("The string that converts the network resource address is:"+decodeURI(encodeURI("http://127.0.0.1/?name=test"))+ "<br><br>");</script>
4. Operation results
(1) Use the parseInt() function:
123abc conversion result is: 123
Abc123 conversion result is:NaN(2) Use the parseFloat() function:
123.456789abc conversion result is: 123.456789(3) Use the isNaN() function:
123.456789 Is it NaN after conversion?
Is the abc123 conversion result NaN: true(4) Use isFinite() function
Is the result of dividing 1 by 0 infinite?(5) Use the encodeURI() function
Converted to the network resource address as: http://127.0.0.1/?name=%E6%B5%8B%E8%AF%95(6) Use the decodeURI() function
The string that converts the network resource address is: http://127.0.0.1/?name=test
For more information about JavaScript, please view the special topic of this site: "Summary of common JavaScript functions techniques》、《JavaScript object-oriented tutorial》、《Summary of JavaScript Errors and Debugging Skills》、《Summary of JavaScript data structure and algorithm techniques"and"Summary of JavaScript mathematical operations usage》
I hope this article will be helpful to everyone's JavaScript programming.