Front end development_ JavaScript_ Functions_ System function

System function

1. Introduction

We know that functions are code blocks that can realize specific functions. In order to simplify development, JavaScript has specially defined some common functions for developers to use. These functions are the system functions of JavaScript. (to put it bluntly, it is a function written by others for us to use.).

2. System function classification

Common system functions include parseXxx() function, isNaN() function, eval() function, Number() function, String() function, etc. Next, we will introduce the use of these functions one by one.

3.parseXxx() function

   (1). summary

The parseXxx() function generally includes parseInt(), parseFloat(), etc. its function is to convert the string to the specified data type. It starts parsing from the beginning of the string, stops parsing at the first non integer position and returns the previously read data. If the beginning of a string does not start with a number, then this time it will return the type: NaN, indicating that it is not a number.

    (2). Example of using parsexxx() function and screenshot of effect

parseXxx()Function syntax:

parseXxx(value);

<script type="text/javascript">
		/**parseXxx()Function usage example**/
		var str1 = "HelloWorld1.23";
		var str2 = "1.23HelloWorld";
		var str3 = "4.56"
		
		//Analytical conversion and output of integers
		document.write(str1 + "Resolve to integer yes:" + parseInt(str1) + "<br>");
		document.write(str2 + "Resolve to integer yes:" + parseInt(str2) + "<br>");
		document.write(str3 + "Resolve to integer yes:" + parseInt(str3) + "<br>");
	    document.write("-----------------------------------"+ "<br>");
		//Analytical output of decimals
		document.write(str1 + "Resolve to integer yes:" + parseFloat(str1) + "<br>");
		document.write(str2 + "Resolve to integer yes:" + parseFloat(str2) + "<br>");
		document.write(str3 + "Resolve to integer yes:" + parseFloat(str3) + "<br>");
</script>

 4.Number() function

   (1). summary

The Number() function converts the value of an object to a number. If this object cannot be converted into a number, the Number() function will return NaN at this time.

    (2).Number() function syntax, usage example, effect screenshot

<script type="text/javascript">
		/**Number()Function usage example**/
		var obj1 = "1.23";
		var obj2 = "1.23,4.56,7.89";
		
		var obj3 = "";
		var obj4 = null;
		
		var obj5 = true;
		var obj6 = false;
		
		var obj7 = "0xabd";
		var obj8 = "2.5e+7";
		
		//array
		var arr = ["1.2"];
		var arr2 = ["1.2","1.3"]
		
		//Parse using the Number() function
		document.write("The result of parsing is:" + Number(obj1) + "<br>");
		document.write("The result of parsing is:" + Number(obj2) + "<br>");
		document.write("The result of parsing is:" + Number(obj3) + "<br>");
		document.write("The result of parsing is:" + Number(obj4) + "<br>");
		document.write("The result of parsing is:" + Number(obj5) + "<br>");
		document.write("The result of parsing is:" + Number(obj6) + "<br>");
		document.write("The result of parsing is:" + Number(obj7) + "<br>");
		document.write("The result of parsing is:" + Number(obj8) + "<br>");
		
		document.write("The result of parsing is:" + Number(arr) + "<br>");
		document.write("The result of parsing is:" + Number(arr2) + "<br>");
</script>

 

 5.isNaN() function

    (1). summary

The isNaN() function checks whether its arguments are non numeric values. If the parameter value is NaN or non numeric values such as string, object and undefined, it returns true; otherwise, it returns false. In fact, it determines whether a value can be legally converted into a number by Number().

   (2). Usage details

①: string parsing in digital form, such as "1.23", "4.5" can be parsed into numbers, and the return value is false, but data similar to "1.2.3", "4.5" and "5.6" cannot be parsed, as shown below

<script type="text/javascript">
/**isNaN()Function usage example**/
var obj1 = "1.23";
var obj2 = "1.23,4.56,7.89";
		
//Parsing using the isNaN() function
document.write(obj1 + "The result of parsing is:--Number()result:" + Number(obj1) +"--isNaN()result:" + isNaN(obj1)+ "<br>");
document.write(obj2 + "The result of parsing is:--Number()result:" + Number(obj2) +"--isNaN()result:" + isNaN(obj2)+ "<br>");
		
</script>

  ②. Null value: null, such as empty string '', empty array, etc. will be converted to 0 when being parsed, so IsNaN will be regarded as a number, and all IsNaN parsing results are false.

<script type="text/javascript">
   var obj1 = "";
   var obj2 = [];
   var obj3 = null;
		
   //Parsing using the isNaN() function
document.write(obj1 + "The result of parsing is:--Number()result:" + Number(obj1) +"--isNaN()result:" + isNaN(obj1)+ "<br>");
document.write(obj2 + "The result of parsing is:--Number()result:" + Number(obj2) +"--isNaN()result:" + isNaN(obj2)+ "<br>");
document.write(obj3 + "The result of parsing is:--Number()result:" + Number(obj3) +"--isNaN()result:" + isNaN(obj3)+ "<br>");

</script>

 ③. For Boolean values, number (true) = 1 and number (false) = 0, so isNaN also returns false for Boolean values.

<script type="text/javascript">
var obj1 = true;
var obj2 = false;
		
//Parsing using the isNaN() function
document.write(obj1 + "The result of parsing is:--Number()result:" + Number(obj1) +"--isNaN()result:" + isNaN(obj1)+ "<br>");
document.write(obj2 + "The result of parsing is:--Number()result:" + Number(obj2) +"--isNaN()result:" + isNaN(obj2)+ "<br>");

</script>

 ④. The number of arrays depends on the number. If the array has only one element, Number() can be parsed into numbers at this time, that is, the result is false. However, if there are multiple numbers, it cannot be parsed at this time, and it is true

<script type="text/javascript">
   var obj1 = ["1.5"];
   var obj2 = ["1.5","2.5"];
		
//Parsing using the isNaN() function
document.write(obj1 + "The result of parsing is:--Number()result:" + Number(obj1) +"--isNaN()result:" + isNaN(obj1)+ "<br>");
document.write(obj2 + "The result of parsing is:--Number()result:" + Number(obj2) +"--isNaN()result:" + isNaN(obj2)+ "<br>");

</script>

  ⑤. Numbers in special forms, such as scientific counting method and data of different mechanisms, will also be parsed into numbers during parsing, as shown below:

<script type="text/javascript">
   var obj1 = "2e+7";
   var obj2 = "0xabc";
		
   //Parsing using the isNaN() function
document.write(obj1 + "The result of parsing is:--Number()result:" + Number(obj1) +"--isNaN()result:" + isNaN(obj1)+ "<br>");
document.write(obj2 + "The result of parsing is:--Number()result:" + Number(obj2) +"--isNaN()result:" + isNaN(obj2)+ "<br>");

</script>

6.eval() function

  (1). summary

The eval() function evaluates the Javascript string and executes it as script code. If the argument is an expression, the eval() function executes the expression. If the parameter is a Javascript statement, eval() executes the Javascript statement.

  (2). The basic use of Eval () function and screenshots of related effects

<script type="text/javascript">
   var x = 10; 
   var str = "2+2";
   var str1 = "x + 7";
   document.write("eval()Function usage test:" + eval(str) + "<br>");
   document.write("eval()Function usage test:" + eval(str1) + "<br>");
</script>

7.string() function

The string() function will be explained in the string topic.

Tags: Front-end Javascript function

Posted by phbock on Sun, 02 Jan 2022 03:22:36 +1030