Home > Web Front-end > JS Tutorial > body text

New APIs for Math, Number, String, Array, and Object

PHP中文网
Release: 2017-07-14 18:10:34
Original
1406 people have browsed it

Math

Math.trunc()

Take out the decimal part of a decimal and return the integer part

<code>Math.trunc(1.239);  //1
Math.trunc(-3,1415926);  //-2
Math.trunc(3.9);  //3</code>
Copy after login

For null values ​​and values ​​that cannot be intercepted as integers, return NaN

<code>Math.trunc("a");  //NaN
Math.trunc();  //NaN
Math.trunc(NaN);  //NaN</code>
Copy after login

Math.sign()

Judge positive number, negative number or 0, return 1 for positive number, -1 for negative number, 0 for 0 and NaN for other values

<code>Math.sign(11.22);  //1
Math.sign(253);  //1
Math.sign(0);  //0
Math.sign(0.0);  //0
Math.sign(-0.0);  //-0
Math.sign(-0);  //-0
Math.sign(-345);  //-1
Math.sign(-2.983958);  //-1
Math.sign('a');  //NaN
Math.sign();  //NaN
Math.sign(NaN);  //NaN</code>
Copy after login

Math.cbrt()

Calculate the cube root of a number and return NaN for other values

<code>Math.cbrt(8);  //2
Math.cbrt(-64);  //-4
Math.cbrt(-27);//3
Math.sign('a');  //NaN</code>
Copy after login

Number.isInteger()

Determine whether a value is an integer. It returns true or it returns false

<code>Number.isInteger(25) // true  
Number.isInteger(25.0) // true  
Number.isInteger(25.1) // false  
Number.isInteger("15") // false  
Number.isInteger(true) // false  </code>
Copy after login

Number.isNaN()

Used to check whether a value is NaN

<code>Number.isNaN(NaN) // true  
Number.isNaN(15) // false  
Number.isNaN('15') // false  
Number.isNaN(true) // false </code>
Copy after login

String

includes()

Used to check whether any connected items in the array are included. If true is included, false is not included

<code>var str="weirenshi";
str.includes("shi")//ture
str.includes("ei")//ture
str.includes("df")//false
str.includes("dfghjk")//false</code>
Copy after login

startsWidth()

Starting with a string letter (one or two) returns true instead of false

<code>var str="weirenshi";
str.startsWidth("w")//ture
str.startsWidth("we")//ture
str.startsWidth("d")//false</code>
Copy after login

endsWidth

Ending with a string letter (one or two) returns true and does not contain false

<code>var str="weirenshi";
str.endsWidth("i")//ture
str.endsWidth("hi")//ture
str.endsWidth("d")//false</code>
Copy after login

repeat

Copy: Define as many as you want and you will copy as many as you want

<code>"abc".repeat(3) // "abcabcabc"
"wei".repeat(5) // "weiweiweiweiwei"</code>
Copy after login

Array

Array.from is copied to an array and copied unchanged

<code>var arr=[1,2,3,4,5];
var ass=Array.from(arr);//[1,2,3,4,5]</code>
Copy after login

Array.of put into an array

Similar to new Array

<code>Array.of(1, 2, 3);//[1,2,3]</code>
Copy after login

fill

1 means starting from the first position, 7 means starting from the first position and followed by 7

<code>[0, 0, 0].fill(7, 1) // [0,7,7]
[0, 0, 0, 4, 6, 3, 4].fill(9, 3) // [0,0,9,9,9,9,9]</code>
Copy after login

Object

Object.assign shallow copy The first parameter is the target object, and the following parameters are the source object

<code>var aa = { a: 1 };  
var qq = { b: 2 };  
var zz = { c: 3 };  
Object.assign(aa, qq, zz);  
target // {a:1, b:2, c:3} 

var ss={x:1,y:2};
var bb={};
Object.assign(ss,bb);
bb.x=3;
ss//x:1,y:2;
bb//x:3,y:2;</code>
Copy after login

Copy directly and merge objects

<code>var ff={x:1,y:2};
var vv={k:9,l:8};
var kk=Object.assign(ff,vv)//{x:1,y:2,k:9,l:8}</code>
Copy after login

The above is the detailed content of New APIs for Math, Number, String, Array, and Object. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template