var str1 = "Hello "; var str2 = "world!"; var n = str1.concat(str2); Hello world! 輸出結果
注意: every() 不會對空數組進行檢測。數組
注意: every() 不會改變原始數組。app
例子:函數
var ages = [32, 33, 16, 40]; function checkAdult(age) { return age >= 18; } function myFunction() { document.getElementById("demo").innerHTML = ages.every(checkAdult); } false 輸出結果
var ages = [32, 33, 16, 40]; function checkAdult(age) { return age >= 18; } function myFunction() { document.getElementById("demo").innerHTML = ages.filter(checkAdult); } 32,33,40 輸出結果
<button onclick="numbers.forEach(myFunction)">點我</button> <p id="demo"></p> <script> demoP = document.getElementById("demo"); var numbers = [4, 9, 16, 25]; function myFunction(item, index) { demoP.innerHTML = demoP.innerHTML + "index[" + index + "]: " + item + "<br>"; } </script> 輸出結果 index[0]: 4 index[1]: 9 index[2]: 16 index[3]: 25
var fruits = ["Banana", "Orange", "Apple", "Mango"]; var a = fruits.indexOf("Apple"); 結果 2
var fruits = ["Banana", "Orange", "Apple", "Mango"]; var energy = fruits.join(); 結果 Banana,Orange,Apple,Mango
var fruits = ["Banana", "Orange", "Apple", "Mango"]; var a = fruits.lastIndexOf("Apple"); 結果 2
var numbers = [4, 9, 16, 25]; function myFunction() { x = document.getElementById("demo") x.innerHTML = numbers.map(Math.sqrt); } 結果 2,3,4,5
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.pop(); 返回 Banana,Orange,Apple
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.push("Kiwi") 結果輸出 Banana,Orange,Apple,Mango,Kiwi
var numbers = [65, 44, 12, 4]; function getSum(total, num) { return total + num; } function myFunction(item) { document.getElementById("demo").innerHTML = numbers.reduce(getSum); } 輸出結果 125
var numbers = [65, 44, 12, 4]; function getSum(total, num) { return total + num; } function myFunction(item) { document.getElementById("demo").innerHTML = numbers.reduceRight(getSum); } 輸出結果 125
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.reverse(); 輸出結果 Mango,Apple,Orange,Banana
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.shift() 結果 Orange,Apple,Mango
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; var citrus = fruits.slice(1,3); 結果輸出 Orange,Lemon
var ages = [3, 10, 18, 20]; function checkAdult(age) { return age >= 18; } function myFunction() { document.getElementById("demo").innerHTML = ages.some(checkAdult); } 輸出結果 true
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.sort(); 輸出結果 Apple,Banana,Mango,Orange
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.splice(2,0,"Lemon","Kiwi"); 輸出結果 Banana,Orange,Lemon,Kiwi,Apple,Mango
var d=new Date(); var n=d.toLocaleString(); 輸出結果 2019/11/7 下午4:59:41
var d=new Date(); var n=d.toString();
Thu Nov 07 2019 17:00:09 GMT+0800 (中國標準時間)
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.unshift("Lemon","Pineapple"); fruits 將輸出: Lemon,Pineapple,Banana,Orange,Apple,Mango
做者:高瑞廷測試
QQ:2419056691ui