[JavaScript] js Basic Syntax - 2 (Arrays, Functions, Global Variables)

Posted by k3Bobos on Fri, 04 Oct 2019 13:33:14 +0200

Article directory

1. js array

1.Definition:
(1)var arr = [1,2,3]; var arr = [1,"4",true];
(2)Using built-in objects Array
	var arr1 = new Array(5);//Define an array with an array length of 5
	arr1[0] = "1";
(3)Using built-in objects Array
	var arr2 = new Array(3,4,5);//Define an array with elements of 3,4,5
2.Gets the length of the array: length
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script type="text/javascript">
    //The first way to define arrays
    var arr = [1,2,3];
    var arr1 = [1,"kk",true];
    alert(arr.length);

    //The second way to define arrays
    var arr2 = new Array(3);
    arr2[0]="a";
    arr2[1]="b";
    arr2[2]="c";
    alert(arr2);

    //The third way to define arrays
    var arr3 = new Array(3,4,5);
    alert(arr3);
    //Array length
    alert(arr3.length);
</script>
</body>
</html>

2. Functions of JS

1. There are three ways to define functions in js:
(1) Use a keyword function
	function method name (parameter list){
		Method body;
		The return value is dispensable, according to actual needs.
	}
(2) Anonymous functions
	var add = function (parameter list){
		Method body and return value;
	}
(3) Use the built-in object in js: Funtion
	var add = new Function ("parameter list", "method body" and return value");

The first way

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script type="text/javascript">
    //Create functions in the first way
    function test() {
        alert("aaaa");
    }
    //Calling method
    test();

    //Define a parametric method to add two numbers
    function add(a,b) {
        var sum = a+b;
        alert(sum);
    }
    add(1,2);

    //Define a method with a return value
    function add1(a,b,c) {
        var sum1 = a+b+c;
        return sum1;
    }
    alert(add1(3,4,5));
</script>
</body>
</html>

The second three ways

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script type="text/javascript">
    //Create functions in the second way
    var add1 = function (m,n) {
        alert(m+n);
    }
    //Calling method
    add1(5,6);

    //Create functions in the second way
    canshu="x,y";
    fangfati="var sum;sum=x+y;return sum;";
    var add2 = new Function(canshu,fangfati)
    alert(add2(2,3));
</script>
</body>
</html>

3. Global and local variables of JS

Global variables: Define a variable in the script tag that can be used in the js section of the page 
Local variables: Define a variable within the method and use it only within the method

global variable

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script type="text/javascript">
    //Create functions in the second way
    var add1 = function (m,n) {
        alert(m+n);
    }
    //Calling method
    add1(5,6);

    //Create functions in the second way
    canshu="x,y";
    fangfati="var sum;sum=x+y;return sum;";
    var add2 = new Function(canshu,fangfati)
    alert(add2(2,3));
</script>
</body>
</html>

local variable

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script type="text/javascript">
    function test1() {
        var a = 10;
        alert("Within the methodology:"+a);
    }
    test1();
    //Press F12 under ie browser to see the console display error
    alert("Outside the method:"+a);
</script>
</body>
</html>

Script tags are generally available anywhere, but it is recommended that script tags be placed at the back.

Topics: Programming Javascript IE