HackMD
  • API
    API  HackMD API beta testing
    HackMD API is now in beta, join us for a test drive.
    Getting started Got it
      • Create new note
      • Create a note from template
    • API  HackMD API beta testing
      API  HackMD API beta testing
      HackMD API is now in beta, join us for a test drive.
      Getting started Got it
      • Options
      • Versions and GitHub Sync
      • Transfer ownership
      • Delete this note
      • Template
      • Save as template
      • Insert from template
      • Export
      • Dropbox
      • Google Drive
      • Gist
      • Import
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
      • Download
      • Markdown
      • HTML
      • Raw HTML
      • ODF (Beta)
      • Sharing Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • More (Comment, Invitee)
      • Publishing
        Everyone on the web can find and read all notes of this public team.
        After the note is published, everyone on the web can find and read this note.
        See all published notes on profile page.
      • Commenting Enable
        Disabled Forbidden Owners Signed-in users Everyone
      • Permission
        • Forbidden
        • Owners
        • Signed-in users
        • Everyone
      • Invitee
      • No invitee
    Menu Sharing Create Help
    Create Create new note Create a note from template
    Menu
    Options
    Versions and GitHub Sync Transfer ownership Delete this note
    Export
    Dropbox Google Drive Gist
    Import
    Dropbox Google Drive Gist Clipboard
    Download
    Markdown HTML Raw HTML ODF (Beta)
    Back
    Sharing
    Sharing Link copied
    /edit
    View mode
    • Edit mode
    • View mode
    • Book mode
    • Slide mode
    Edit mode View mode Book mode Slide mode
    Note Permission
    Read
    Owners
    • Owners
    • Signed-in users
    • Everyone
    Owners Signed-in users Everyone
    Write
    Owners
    • Owners
    • Signed-in users
    • Everyone
    Owners Signed-in users Everyone
    More (Comment, Invitee)
    Publishing
    Everyone on the web can find and read all notes of this public team.
    After the note is published, everyone on the web can find and read this note.
    See all published notes on profile page.
    More (Comment, Invitee)
    Commenting Enable
    Disabled Forbidden Owners Signed-in users Everyone
    Permission
    Owners
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Invitee
    No invitee
       owned this note    owned this note      
    Published Linked with GitHub
    Like BookmarkBookmarked
    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

    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 lost their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template is not available.


    Upgrade

    All
    • All
    • Team
    No template found.

    Create custom template


    Upgrade

    Delete template

    Do you really want to delete this template?

    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 via Google

    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

    Tutorials

    Book Mode Tutorial

    Slide Mode Tutorial

    YAML Metadata

    Contacts

    Facebook

    Twitter

    Feedback

    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

    Versions and GitHub Sync

    Sign in to link this note to GitHub Learn more
    This note is not linked with GitHub Learn more
     
    Add badge Pull Push GitHub Link Settings
    Upgrade now

    Version named by    

    More Less
    • Edit
    • Delete

    Note content is identical to the latest version.
    Compare with
      Choose a version
      No search result
      Version not found

    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. Learn more

         Sign in to GitHub

        HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.

        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
        Available push count

        Upgrade

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Upgrade

        Danger Zone

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

        Syncing

        Push failed

        Push successfully