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
    # [Day03] 基本型別 ###### tags: `鐵人賽` ##### 上一篇提到 JavaScript 是弱型別語言。 強型別語言在變數被宣告的時候必須指定資料型別給它,如果對變數做了錯誤型別的運算則會出現錯誤,優點是能減少在執行時期(Runtime)發生的錯誤。 而弱型別的 JavaScript 剛好相反,變數本身不帶有資料型別的資訊,其中的**值**或**物件**才有。在執行時期透過變數內容來參考至**物件**或**值**,才會知道變數有什麼操作方式。取得語法簡潔的優點,但要注意**型態轉換**時產生**非預期**的問題。 JavaScript 主要有分成基本型別(Primitives)及物件型別(Object)兩大類型別。可以用 typeof 運算子來做判斷型別的方式。 ## 基本型別Primitives: 1. **string**(字串) JS 只有字串沒有字元的概念,所以要把字串包起來,可以用雙引號 "" 或單引號 '',或者 ES6 新增樣板字面值(template literal)用反引號 \`\` 包覆,看個人喜好使用。 ```javascript= var str1 = "Let's go!";// 雙引號 var str2 = 'Let\'s go!';// 單引號遇到錯誤時在前面加\(escape character 跳脫字元) var str3 = `Let's go!`;// 反引號 var str4 = "Hello, " + "world.";//用+連接字串 console.log(str4) Hello, world. ``` 用`+`在字串加上變數: ```javascript= var age = 25; var str5 = "I'm " + age + " years old."; console.log(str5) I'm 25 years old. var str6 = `I'm ${age} years old.`;// 用樣板字面值加上變數的用法 console.log(str6) I'm 25 years old. ``` 過去斷行與樣板字面值的差異 ```javascript= var hello1 = "這不是一行文\n這是第二行\n這是第三行"; console.log('10', hello1) 10 這不是一行文 這是第二行 這是第三行 var hello2 = `這不是一行文 這是第二行 這是第三行`;// es6後用反引號可以直接斷行 console.log('11', hello2); 11 這不是一行文 這是第二行 這是第三行 ``` 2. **number**(數字) 包含整數和帶有小數點的浮點數字,還有幾個比較特殊的數字 Infinity(無限大)、-Infinity (負無限大)及 **NaN** (不是數值,**Not a Number**) 。例:0/0 是 NaN,Infinity/Infinity 也是 NaN 一樣的概念,但有趣的是 typeof NaN,會回報 number。 既然NaN不好判斷那怎麼辦,可以用 isNaN(value) 來判斷 ```javascript= isNaN(NaN); // true isNaN(123); // false isNaN("123"); // false, 字串被轉成數字 isNaN("NaN"); // true, 轉成數字但不是數字 ``` 但是用 isNaN(value)這個方法會透過隱含的 Number()轉成數字,所以後來會建議用 Number.isNaN(value)來取代,可以參考 **[MDN](https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN#Examples)** 上的差異。 「帶有小數點的浮點數字」,是什麼意思呢? 試著輸入 0.1+0.2=... 的結果我驚呆了 ```javascript= 0.1+0.2=0.30000000000000004 0.1+0.2===0.3; // false ``` 因為 JS 的 number 是基於 「IEEE 754」二進位浮點算數標準,十進位的小數無法完美的轉換成二進位表示,只能用無限循環的位數來趨近於十位數的小數(IEEE 745 規定 24 位數上限),所以會省略一些位數,導致還原時小數不夠精準。~~二進位的世界啊!門外漢的全新體驗!~~ 3. **boolean**(布林值) 只有兩種,分別是 ture 以及 false。 通常用在判斷式,作為控制程式流程的用途。 4. **null** (沒有值) 明確代表此變數沒有值。 5. **undefined** 尚未給值, 未定義。 ```javascript= var a = null; // null var b; // undefined // 008 天的舉例很清楚的解釋兩者的區別 ``` 6. **symbol**(es6新增) 型別先到這邊, ## 下一篇 物件型別

    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