JavaScript 基礎

Number

%:除法取餘數

>43%10
3

String

\t:tab空格

>"I have a\tapple"
"I have a   apple"

display “” in output

>""I have a \"apple\"""
"I have a "apple""

\n:斷行

>"I have a \napple"
"I have a 
apple"

.length:計算字數(含空格)

>"I have an apple".length
15
//第一個字元為1,數字的算法跟charAt()不同,所以總數會比charAt()多1

Methods

.charAt():傳來字串中的第某個字

  1. 第一個字元為0,從0開始算
  2. 空格算一個字元
  3. 符號也算一個字元
>var sentence="I have an apple"
>sentence.charAt(3)
"a"
//0:"I"
//1:""
//2:"h"
//3:"a"

Others

內引js檔
<script src="../scripts/trains.js"></script>
  1. 起點是index.html (沒寫出來)
  2. 【..】表示跳到上面資料夾
  3. scripts/ 找到 scripts 資料夾裡面的 trains.js

 

Leave a Reply

Your email address will not be published. Required fields are marked *