Date: Sep. 15th, 2019
Lecturer: Chia
版本 | 正式名稱 | 備註 |
---|---|---|
1 | ECMAScript 1 (1997) | First Edition. |
3 | ECMAScript 3 (1999) | 廣泛支援 (2000 - 2010) |
5 | ECMAScript 5 (2009) | JS ES5 |
6 | ECMAScript 2015 | JS ES6 |
8 | ECMAScript 2017 | 添加 Object |
9 | ECMAScript 2018 | 最新版 |
因此,變數本身無需宣告型別!
String
Boolean
Null
Undefined
JS ES5 標準,JavaScript 內建型別:
var x = 3.14;
var x = 123e5;
var y = 123e-5;
console.log(typeof NaN); //number
NaN === NaN; // false
NaN 並不等於任何的數字,甚至是自己。
運算子
+ - * / %
運算順序
console.log("Lie on the ocean");
console.log('Float on the ocean');
console.log(`Down on the sea`);
console.log('這不是一行文 \
這是第二行');
console.log("con" + "cat" + "e" + "nate");
console.log(`half of 100 is ${100 / 2}`);
外部:必須由 ` ` 包裹。
內部:由 $ 及 { } 構成佔位符 ( ${expression} ),允許字串嵌入運算式並返回運算結果。
console.log(3 > 2);
console.log("a" < "Z");
console.log(true && false);
console.log(false || true);
console.log(!false);
console.log(typeof undefined);
Boolean(undefined) //false
console.log(typeof null);
Boolean(null) //false
var a;
console.log(typeof a); //undefined
var a = "";
console.log(typeof a); //string
var b = null;
console.log(typeof b); //object
console.log(8 * null); //null => 0
console.log("5" - 1); //5 (from string to number)
console.log("5" + 1); //1 (from number to string)
console.log("five" * 2);
// → NaN
console.log(false == 0 && "" == false);
// → true
console.log(null == 0);
// → false
console.log(null == undefined);
//true
console.log(null === undefined);
//false (型別不同,直接回傳 fasle)
let a = 5 * 5;
console.log(a); // → 25
let mood = "light";
mood = "dark";
console.log(mood); // → dark
const greeting = "Hello ";
greeting = "a";
console.log(greeting); //Error
宣告常數,不能再作修改。
宣告區域變數。
var name = "Ayda";
const greeting = "Hello ";
console.log(greeting + name); // → Hello Ayda
break case catch class const continue debugger default
delete do else enum export extends false finally for
function if implements import interface in instanceof let
new package private protected public return static super
switch this throw true try typeof var void while with yield
JavaScript 區分大小寫。
let Num = Number(prompt("輸入數字:"));
console.log("平方後的結果:" + Num * Num);
let Num = Number(prompt("輸入數字:"));
if (!Number.isNaN(Num)) {
console.log("平方後的結果:" + Num * Num);
}
if (1 + 1 == 2) console.log("It's true");
// → It's true
let Num = Number(prompt("輸入數字:"));
if (!Number.isNaN(Num)) {
console.log("平方後的結果:" + Num * Num);
} else {
console.log("Hey. Why didn't you give me a number?");
}
let Num = Number(prompt("輸入數字:"));
if (Num < 10) {
console.log("Small");
} else if (Num < 100) {
console.log("Medium");
} else {
console.log("Large");
}
let number = 0;
while (number < 8) {
console.log(number);
number = number + 2;
}
let yourName;
do {
yourName = prompt("Who are you?");
} while (!yourName);
console.log(yourName);
for (let num = 0; num < 7; num = num + 2) {
console.log(num);
}
// → 0, 2, 4, 6
let result = 1;
for (let counter = 0; counter < 10; counter++) {
result = result * 2;
}
console.log(result);
// → 1024
for (let current = 20; current < 30; current ++) {
if (current % 2 == 0) {
continue;
}
console.log(current);
}
// → 21, 23, 25, 27, 29
for (let current = 20; ; current ++) {
if (current % 7 == 0) {
console.log(current); break;
}
}
// → 21
Uses console.log to print all the numbers from 1 to 100, with three exceptions.
numbers / 3 => print "Fizz"
numbers / 5 (and not 3) => print "Buzz"
numbers / 15 => print "FizzBuzz"
else => print numbers
for (let n = 1; n <= 100; n++) {
let output = "";
if (n % 3 == 0) {
output += "Fizz";
}
if (n % 5 == 0) {
output += "Buzz";
}
console.log(output || n);
}
switch (prompt("What is the weather like?")) {
case "rainy":
console.log("Remember to bring an umbrella.");
break;
case "sunny":
console.log("Dress lightly.");
case "cloudy":
console.log("Go outside.");
break;
default:
console.log("Unknown weather type!");
break;
}
函式的名稱 (也可能沒有名稱)
( ) 中的部分為「參數」。多個參數,用 , 隔開。
{ } 內的部分,放需要重複執行的運算。
function square(number) {
return number * number;
}
square(2); // 4
function 名稱([參數]) {
...
}
function square(x) {
return x * x;
}
//匿名函式
變數名稱 = function ([參數]) {
...
}
const makeNoise = function() {
console.log("Pling!");
};
makeNoise(); // → Pling!
console.log(typeof makeNoise); //function
//當箭頭函式無參數時,()仍保留。
變數名稱 = ([參數]) => {
...
};
const power = (base, exponent) => {
let result = 1;
for (let count = 0; count < exponent; count++) {
result *= base;
}
return result;
};
Write a function min that takes two arguments and returns their minimum.
使用 3 種定義函式的方式,分別寫出 min 函式。
// Your code here.
console.log(min(0, 10));
// → 0
console.log(min(0, -10));
// → -10
function min(a, b) {
if (a < b){
return a;
}else{
return b;
}
}
console.log(min(0, 10));
// → 0
console.log(min(0, -10));
// → -10
const min = function(a, b) {
if (a < b){
return a;
}else{
return b;
}
};
console.log(min(0, 10));
// → 0
console.log(min(0, -10));
// → -10
let min = (a, b) => {
if (a < b){
return a;
}else{
return b;
}
}
console.log(min(0, 10));
// → 0
console.log(min(0, -10));
// → -10
名稱([參數]);
const square = function(x) {
return x * x;
};
square(3); //9
let x = 10; //這裡的x為global
if (true) {
let y = 20;
var z = 30;
console.log(x + y + z); // → 60
}
// y is not visible here
console.log(x + z); // → 40
// 狗的計數函式
var count = 0;
function countDogs () {
count += 1;
console.log(count + ' dog(s)');
};
// 貓的計數函式
var count = 0;
function countCats () {
count += 1;
console.log(count + ' cat(s)');
};
countCats();
countCats();
countDogs();
countDogs();
countDogs();
countCats();
// 狗的計數程式
function dogHouse () {
var count = 0;
function countDogs () {
count += 1;
console.log(count + ' dog(s)');
}
return countDogs;
};
const a = dogHouse();
// 貓的計數函式
function catHouse () {
var count = 0;
function countCats () {
count += 1;
console.log(count + ' cat(s)');
}
return countCats;
};
const b = catHouse();
b(); // 1 cat(s)
b(); // 2 cat(s)
a(); // 1 dog(s)
a(); // 2 dog(s)
a(); // 3 dog(s)
b(); // 3 cat(s)
// 狗的計數程式
function dogHouse () {
var count = 0;
return function () {
count += 1;
console.log(count + ' dog(s)');
}
};
const countDogs = dogHouse();
// 貓的計數函式
function catHouse () {
var count = 0;
return function () {
count += 1;
console.log(count + ' cat(s)');
}
};
const countCats = catHouse();
countCats(); // 1 cat(s)
countCats(); // 2 cat(s)
countDogs(); // 1 dog(s)
countDogs(); // 2 dog(s)
countDogs(); // 3 dog(s)
countCats(); // 3 cat(s)
function dogHouse () {
var count = 0;
function countDogs () {
count += 1;
console.log(count + ' dog(s)');
}
return countDogs;
};
var countGolden = dogHouse();
var countBlack = dogHouse();
var countPuppy = dogHouse();
countBlack(); // 1 dog(s)
countBlack(); // 2 dog(s)
countPuppy(); // 1 dog(s)
countBlack(); // 3 dog(s)
countGolden(); // 1 dog(s)
countPuppy(); // 2 dog(s)
countGolden(); // 2 dog(s)
缺點:以遞迴的方式執行程式,其速度比迴圈慢三倍。
function power(base, exponent) {
if (exponent == 0) {
return 1;
} else {
return base * power(base, exponent - 1);
}
};
console.log(power(2, 3)); // → 8