js basic tutorial

node v10.24.1
version: 2.0.0
endpointsharetweet
var x,y; y = 1; x = y + 1; console.log(x); //結果會得出x為2 //因為x,y是可變的所以稱為變數,在js code x,y需要定義如上
//這裡定義函式1加到10 function sum1to10(){ var result = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10; return result; } //呼叫函式 var call = sum1to10(); console.log(call);
//這裡定義函式1加到10,採用傳入輸入參數 function sum1to10ByArgs(start,end){ var result = 0; for (var i = start;i <= end;i++){ result = result + 1; } return result; } //呼叫函式 var callResult = sum1to10(1,5); console.log(call);
//用if ... else判斷傳入的變數是:1或2或3 function checkNumber(no){ if (no == 1){ console.log("傳入1"); }else if (no == 2){ console.log("傳入2"); }else if (no == 3){ console.log("傳入3"); }else { console.log("傳入的值超出範圍:1~3"); } } checkNumber(2);
//改用switch ... case判斷傳入的變數是:1或2或3 function checkNumberBySwitch(no){ switch (no){ case 1: console.log("傳入1"); break; case 2: console.log("傳入2"); break; case 3: console.log("傳入3"); break; default: console.log("傳入的值超出範圍:1~3"); } } checkNumberBySwitch(3);
//字串相加 function addString(name){ return "Hello " + name; } console.log(addString("Louis")); console.log(addString("Stanely"));
//字串比較,大小寫是不一樣的 function checkString(name){ if (name == "Louis"){ console.log("你是Louis"); }else{ console.log("你不是Louis"); } } //首字母小寫 checkString("louis"); //首字母小寫 checkString("Louis");
//array feature const _ = require("lodash"); var arrayObj = [1,2,3,4]; var result = _.find(arrayObj,function(item){ return item == 2; }) console.log(result);
Loading…

no comments

    sign in to comment