# JavaScript - 字串型別與轉換 ###### tags: `javascript` ## 宣告變數與印出值 ```JavaScript= let a = 1; let b = 2; console.log(a); console.log(b); console.log(a, b); alert(a); ``` ## 宣告字串 ```javascript= let hello = "你好嗎?"; console.log(hello); let 去玩 = "let's go!"; console.log(去玩); ``` ## 字串相加 ```javascript= let friendName = 'Joey'; //string let content = 'How do you doin?'; let total = friendName + ' ' + content; console.log(total); let age = 25; //number let ageString = "25"; //string ``` ## 透過 typeof 瞭解當前變數型別 ```javascript= console.log(typeof friendName); //string console.log(typeof age); //number console.log(typeof ageString); //string ``` ## 數字與字串相加 ```javascript= let myName = 'Ross'; //string let myAge = 28; //number console.log(typeof myName); console.log(typeof myAge); // JavaScript 有些情況會貼心自動轉型 let myTotal = myName + myAge; console.log(typeof myTotal); //string console.log("Hi 我是 " + myName + ",我今年 " + myAge + " 歲"); ``` ## NaN 非數字(但型別屬number) ```javascript= let herName = 'Racheal'; //string let herAge = 26; //number let herTotal = herName * herAge; console.log(herTotal); //NaN console.log(typeof herTotal); //number ``` ## 字串轉成數字 parseInt() **parseInt()** ```javascript= // 字串 // let weight = '50'; // 字串轉成數字 let weight = parseInt('50'); console.log(typeof weight); //number // 字串內容無法傳成數字 => NaN let hi = parseInt('hello'); console.log(hi); //NaN console.log(typeof hi); //number ``` ## 字元長度查詢 .length **.length** ```javascript= let car = 'Porsche'; car.length; //7 let z = "你好嗎"; z.length; //3 ``` ## 取出內容"濾掉空白字元" .trim() **.trim()** ```javascript= let myCar = ' KIA '; myCar.length; //5 myCar; //' KIA ' myCar.trim(); //'KIA' let email = " aaa@gmail.com"; email; //' aaa@gmail.com' email.trim(); //'aaa@gmail.com' ``` ## 變數記憶體指向,儲存值需「賦予」= ```javascript= // 變數記憶體指向 let myEmail = ' apple@gmail.com'; console.log(myEmail); // ' apple@gmail.com' myEmail.length; console.log(myEmail.length); // 17 // 濾掉空白字元 console.log(myEmail.trim()); // 'apple@gmail.com' console.log(myEmail); // ' apple@gmail.com' // 「賦予」新的變數以儲存處理後的結果 let updateEmail = myEmail.trim(); console.log(myEmail); // ' apple@gmail.com' console.log(updateEmail); // 'apple@gmail.com' console.log(updateEmail.length); // 15 // 儲存值需「賦予」值 // 宣告變數 let emailLength = myEmail.length; console.log(emailLength); // 17 ``` ## ES6新語法 - 樣版字面值(Template literals) ```javascript= let myName = 'Ross'; let myAge = 28; // let text = "您好,我是" + myName + "我今年" + myAge + "歲"; let text = `您好,我是${myName},我今年${myAge}歲`; // ES6新語法 console.log(text); // 您好,我是Ross,我今年28歲 ``` ## 原始型別: 字串 / 數字 / 布林 / undefined / null ### boolean 布林值 - true / false ```javascript= let isVip = true; console.log(isVip); //true console.log(typeof isVip); //boolean console.log(1 > 2); ``` ### undefined 尚未被賦值 ```javascript= let y; // y = 1; console.log(y); //undefined console.log(typeof y); //undefined ``` ### null 有被賦予值,是告知為空值 * 原本有值,被清掉 => 空值 (釋放記憶體空間) ```javascript= let cart1 = 1; let cart2 = 1234; cart2 = null; console.log(cart1, cart2); // 1 null ``` ## 型別轉換 ### 字串轉成數字型別的方法 parseInt() **parseInt()** ```javascript= let mac = "1"; mac = parseInt(mac); // 轉型別 console.log(mac); console.log(mac + 1); ``` ### 轉型別內容非數字 => 轉型別失敗 => NaN ```javascript= mac = parseInt("小明"); // 非數字轉型別 console.log(mac); // NaN console.log(mac + 1); // NaN ``` * 從文字欄位取出的內容都是字串,因此如果資料需進行數字運算,就須先轉換型別! ### 數字轉成字串型別的方法 .toString() **.toString()** ```javascript= let phone = 123; phone = phone.toString(); console.log(phone); console.log(phone + 1); ``` * 字串相加的使用情境:大樂透號碼、電話區碼"07"+"23456789" ## 【觀念整理】字串相加與相乘 >當使用加法運算子("+")時,只要有一個字串出現,就會將其餘的部分一併轉為"字串"再做運算 所以 "1" + "1" 會等於 "11",也就是兩個字串會合併 ```javascript= let a = "1" + "1"; '11' ``` >但相乘就不一樣,相乘是為了做"數學運算"使用的,所以它會先轉成"數字"再做相乘 ```javascript= let a = "1" * "1"; 1 ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up