JavaScript Basic: Five Primitive

作者: 此之木 | 来源:发表于2019-05-27 05:59 被阅读3次

    JavaScript (JS) is a programming language which can add logic and interactivity to a page. If we treat HTML as a noun and treat CSS as a adjective in a sentence, we can treat JS as a verb which is the one to take action.

    Today, I learned five primitive datatypes about JS.

    Numbers

    Numbers are very simple, just like 4, 10, -29.etc

    It can do some math. For example we type a simple math in the console (open any web page on Google Chrome, right click and inspect the page, click “console” next to the “Element” bar):

    I type “4+26” and the result is calculated automatically.

    The JS follows the order of operations that all regular math follows as well. For example, (3+5)*8=8*8=64.

    Besides simple math, JS can also do modulo which is a reminder operator. It has “%” sign between two numbers. For example:

    The result shows when 10 divide 3, the reminder is 1 (3*3+1=10).

    Strings

    They can be text inside single or double quotes

    For example “Hello world”

    They can be concatenation

    They can escape characters start with “\”

    Pay attention: we need to have a double quote for the whole text, and “\” between the inside double quote text.

    They have a length property

    Basically, it counts how many characters in the text. 

    They can access individual characters by using [] and an index. 

    The third character of “hello” is “l”.

    Variables (Booleans)

    Variables are simple containers that store values. They follow this pattern:

    Var your variableName = yourValue;

    They can store all of the values we’ve seen

    For example:

    var name = “Lilly”;var number = 20;

    They can recall the stored value by calling the variable name

    Once we type our first var name “Lilly” in the JS, it already store the data. When we type “name”, it will recall the data “Lilly”.

    So does the number, too. Since the JS has stored the number “20”, when we use number to do the simple math, it will apply the number in the math operation.

    We can also update existing variables

    When we update a new name “Kitty”, JS will also update the existing name “Lilly”.

    Null 

    Null is “explicitly nothing”. Null expresses a lack of identification, indicating that a variable points to no object. (From MDN)


    Undefined

    Undefined refers to variables that are declared but not initialized.

    Since we don’t give any value of those variables, the result is undefined. 

    相关文章

      网友评论

        本文标题:JavaScript Basic: Five Primitive

        本文链接:https://www.haomeiwen.com/subject/fkfxoqtx.html