ifchang
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # [Day02] 變數 ###### tags: `鐵人賽` # 變數 變數是用來儲存資料和進行基本運算的基本單位;在宣告時給資料一個名稱,名稱像一個盒子把資料裝起來,需要用到的時候找盒子就可以拿到資料。 ```javascript= var a = 10; var b = 20; var c = 30; ``` JavaScript 是一個**弱型別**的語言,變數在宣告時不用指定型別,型別的資訊只在**值**或**物件**本身,變數只用來作為取得值或物件的參考 ```javascript= var d; // 因宣告 d 沒給數值,所以 d 的內容為 undefined ``` 可以在瀏覽器的 Devtools(工程模式)用 console.log(變數名)印出我們宣告過的變數 a、b、c、d 再加一個為宣告的 e,會得到如下圖 ![](https://i.imgur.com/pCdKGoV.png) 可以看到 a、b、c 皆出現該有的值,未賦值的 d 顯示值為 undefined,e 因為未宣告所以報錯。 而對未 var(宣告)的變數也能直接賦值 e = 10 ,但是會有後遺症 ```javascript= e = 10; // 自動變成全域變數 ``` <br> ## 變數命名規則 * 數字不可用於開頭,可以用英文大小寫字元、下底線`_`、錢字號 `$` * 不能用 `-` 跟 `.` * 有區分大小寫,大小寫不同視為不同變數 * 名稱 identifier(識別符,簡稱ID),它在程式碼上下文中具有唯一性。 * 名稱不可以是關鍵字(在 JS 裡有特定用途的字詞,例如 if、else、function 這些) * 名稱不可以是保留字(未有特定用途,但未來可能會變關鍵字詞,~~若是擔心的話~~可參考 [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#keywords) 的說明) 然後有一些是寫 code 習慣上的**注意事項** * 變數與函式建議用小駝峰,類別用大駝峰式的命名 ```javascript= var userAddress = '台南某區某路某號'; // 小駝峰第二字字首大寫 var UserNumber = '0912345678'; // 大駝峰字首皆大寫 ``` * 雖然支援 Unicode (JavaScript 1.3 後),但基於開發習慣,盡量避免使用非英文字母、下底線 `_`、錢字號 `$` 的字元命名變數,以免其他語系的系統打開變成亂碼 * 下底線 `_`、錢字號 `$` 為開頭的命名通常是有特殊用途,例:類別中的私有變數、常數或函式(方法) <br> ## var、let、const 在 ES6 以前可以透過 var 來宣告變數,而 ES6 以後宣告分成了變數與常數 | ES6 以前 | ES6 以後 | | --- | ---- | | 變數 var | 變數 var、let<br>常數 const | var 過的值可以一直 var 下去,但 let 不行; ```javascript= var a = 3; var a = 4; var a = 5; let b = 6; let b = 7; // 報錯情況 let b = 6; ^ SyntaxError: Identifier 'b' has already been declared ``` var 與 let 最大的差異在於變數作用範圍(scope)的不同. > **[MDN](https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Statements/let)** > let 可以宣告只能在目前區塊、階段或表達式中作用的變數。而<br> var 則是定義了一個全域變數,或是在整個 function 而不管該區塊範圍。 看的似懂非懂,沒關係我們來寫寫看 ```javascript= let b = '全域 let b'; var b1 = '全域 var b1'; if(true){// Start of Block scope let b = '區塊 let b'; var b1 = '區塊 var b1'; console.log(b); console.log(b1); }// End of Block scope console.log(b); console.log(b1); // 四個 console.log 執行結果 區塊 let b // 區塊內宣告的 let b 區塊 var b1 // 區塊內宣告的 var b1(全域的 var b1 被覆蓋) 全域 let b // 全域宣告的 let b 區塊 var b1 // 區塊內宣告的 var b1(全域的 var b1 被覆蓋) ``` 結論:除了區域跟執行順序也有關係。 <br> ## 常數 const * 宣告 const 常數,會對它的值建立一個唯讀的參考。 並不是說這個值不可變更,而是這個變數不能再一次指定值。例如,假設常數的內容(值)是個物件,那麼此物件的內容(物件的參數)是可以更改的。 <br> ## 下一篇:型別

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    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

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully