JavaScript Basics (1): Basic Syntax

1. Data Type

JavaScript is a weakly typed language and has no strict requirements on how different types of data are used. Commonly used basic data types are:

  • Numerical values : integers and decimals
  • Text data : Text data in js needs to be enclosed in double quotes ("hello world!")
  • Boolean type : Boolean type value is only true&false, indicating true and false

In addition to basic data types, js also has object data types (Object)

2. Variables

2.1 Variables are stored in memory and used to store temporary data. Variable names in js are strictly case-sensitive. Basic naming rules:

  • cannot start with a number
  • Cannot contain special characters: & and %
  • Cannot use reserved words of js: var, with ....

2.2 Creating variables and assigning variables The keyword var
is used in js to declare variables. After declaring variables, variables can be used to store any type of data. Because js is a weakly typed language, there is no need to specify the data type stored by the variable when specifying the variable.

<script type="text/javascript">
        // 声明一个变量,无需指定变量存储的数据类型
        var myVariable;
        // 给变量赋值
        myVariable = "Hello!";
        alert(myVariable);

        // js中变量存储的数据类型可以改变,神奇!
        myVariable = 321;
        alert(myVariable);

        // 声明一个变量但未赋值,此时变量的值是:undefined
        var testVariable;
    </script>

2.3 Data Type Conversion

  • Convert strings to numbers: parseInt() and parseFloat()
  • Handling strings that cannot be converted to numbers: If you try to convert "hello!" to a number, you will get NaN (Not a number). The function isNaN() is used to test whether the string word is a numerical value. It returns true if it is not a numerical value, and returns false if it is a numerical value.

3. Arrays

Unlike variables, arrays can hold multiple data (elements), and arrays distinguish data items in the array by the index value index.

<script type="text/javascript">
        // 声明数组的原始方式,利用new关键字和Array()函数
        var myArray = new Array();
        // 实际开发中使用数组字面值创建数组,声明数组时无需指定数组大小。
        // 数组中每一个元素都可视为一个标准变量,同一数组中可以保存不同类型的数据。。。神奇!
        var myArray = [];
        // 为数组中元素赋值
        myArray[0] = "Paul";
        myArray[1] = 32;
        myArray[2] ="John";
        myArray[3] = 27;

        // 如果程序试图访问数组中未定义的元素,则返回undefined
        alert(myArray[10]);
    </script>

4. Functions and scope

4.1 The main points of the function :

  • Every function defined in js must have a unique name on that page
  • Using the return statement, a function can return a value to the code that called it, otherwise it returns undefined by default
  • A function's code is executed only when it is asked to, called a function call
<script type="text/javascript">
        // 自定义函数,function为关键字
        function convertToCentigrade(degFahren){
            // 声明一个内部变量
            var degCent = 5 / 9 * (degFahren - 32);

            // 返回值
            return degCent;
        }

        // 调用函数
        var temp = convertToCentigrade(32);
    </script>

4.2 Scope
The scope of variables in js is divided into global scope and local scope. Variables in the global scope are declared outside the function and can be referenced by all scripts on the entire page. Therefore, variables in the global scope are easily modified inadvertently and should be avoided as much as possible in actual development.

<script type="text/javascript">
        // 全局变量,定义在函数之外
        var degFahren = 12;

        function convertToCentigrade(degree){
            // 局部变量,定义在函数内部。函数的参数也是局部作用域的。
            var degCent = 5 / 9 * (degree - 32);
        }
    </script>

If the local variable and the global variable have the same name, the js engine will look for the variable in the current scope through the identifier lookup, and the operation priority of the local variable is higher than that of the global variable.

4.3 Using functions as values ​​(the most magical operation of js)
Functions can be used in js like any other value, such as passing a function as a parameter to another function. . .

<script type="text/javascript">
        function convertToCentigrade(degree){
            var degCent = 5 / 9 * (degree - 32);
        }
        // 将函数引用赋值给一个变量...
        //注意函数名称后面没有括号,有括号就变成了函数调用了
        var myFunction = convertToCentigrade;

        function doSomething(fn){
            fn("Helle World!");
        }
        // 将一个函数作为参数传递给另一个函数。alert()
        doSomething(alert);
    </script>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325628435&siteId=291194637