function 函式
函式寫法
function 函式名稱(){
指令
....
}
function speak(){
console.log('Hello');
}
speak();
註冊多組函式
function speak(){
console.log('Hello');
}
function quiet(){
console.log('');
}
speak();
quiet();
函式裡面執行函式
function morningAction(){
console.log('起床');
makeUp();
console.log('出門');
}
function nightAction(){
console.log('洗澡');
makeUp();
console.log('睡覺');
}
function makeUp(){
console.log('刷牙');
console.log('洗臉');
}
morningAction();
nightAction();
函式參數
function number(num){
console.log(num);
}
number(3);
函式參數練習---相加
function addNum(num1,num2){
console.log(`num1+num2=${num1+num2}`)
}
addNum(1,2);
函式搭配return
function addNum(num1,num2){
console.log(`num1+num2=${num1+num2}`)
return num1+num2
}
let total=addNum(1,2);
*return 可以中斷函式執行
練習---偶數判斷
function isEven(num){
if(num%2==0)
return 'isEven';
else
return 'notEven';
}
let test1=isEven(2);
let test2=isEven(5);
console.log(test1);
console.log(test2);