Introduction to core data types and variable usage in JavaScript language _ basic knowledge-js tutorial

Source: Internet
Author: User
Like many programming languages, JavaScript also has its own language core, the core part of the language for understanding and learning JavaScript is the very good start of JavaScript learning. Any programming language has its own language core, just like many programming languages, javaScript also has the core of its own language. The core part of a language is generally called the most basic part of JavaScript. The saying goes that everything is difficult at the beginning. Learning JavaScript is also a truth. There is also an old saying that a good start is half the success. The core part of understanding and learning JavaScript is a very good start for JavaScript learning.

The following code describes the JavaScript data types and variables:

The Code is as follows:


// In the script tag, all the content following // is called annotation.
// Comments are generally used to explain a piece of JavaScript, so that other Web Front-end development engineers can be more clear when reading this code.

// A variable is a symbolic name of a value. Through the names of these variables, we can also roughly know what the variable is for and what type it belongs.
// It is easy to differentiate variables. There is "var" before the variable, that is, the variable is declared through "var.

Var m; // declare a variable m

// Generally, the value to be declared is assigned to the variable with an equal sign.
Var m = 10; // The current variable m is equal to 10

M // use the variable m to obtain the declared value

// Alert (m) // use the alert () function to pop up the m value in the browser

// JavaScript data type: Boolean value, number, String, underfind, function, array, Object

Var n = 1; // number
N = 0.01 // integer and real number are both numerical

Var s = "hello"; // a string consisting of text in double quotation marks
S = 'guo'; // a string consisting of text in single quotes

Var B = false; // The Boolean value of the Error
B = true; // The correct Boolean value.

Var z = null; // a null value, which is a special type and is an object after typeof

Var u; // underfind

Var j = {// an object representing json
Li: 3, // The value of the attribute "li" is 3
Meng: 4 // The value of the attribute "meng" is 4
}

J ["li"] // access the value in json through []
J. li // access the value in json through.
J. long = 5 // create a new property by assigning values.
J. ai = {// you can create a new json object by assigning values.
Xin: 33
}
J. kong = {}// {} indicates an empty object. It has no attribute.

J. ai. xin // access attributes in the new json through.

Var a = [2, 3] // an object representing an array

A [0] // [] in the array represents the position where the number in [] represents, and the array starts from 0, so a [0] is the first element of the array.
A. length // length indicates the number of array a, 3
A [a. length-1] // represents the last element in the array
A [9] = 2; // use the value assignment method to add a new element.

// If a = [], it indicates that the elements in the array are zero, and a. length = 0

A [0] = {
Li: 333
}

// Arrays can be included in json, and arrays can also contain json

// Alert (a [0] ["li"])


In the above code, you can use "[]", "{}", ". "defines the object, you can also use" [] "," {} ",". to change the content of an array or object. You can also use "[]", "{}", and "." to read some data in the object. The following code is related to operators:

The Code is as follows:


// You can use the budget operator in JavaScript to calculate two numbers and generate new values.
// The following are common budget characters, such as "+", "-", "*", and ","-","*","/"

// 1. Operator Introduction
10 + 10 // addition, 20
10*10 // multiplication, 100
10-10 // subtraction, 0
10/10 // Division, 1

Var j = {// an object representing json
Li: 3, // The value of the attribute "li" is 3
Meng: 4 // The value of the attribute "meng" is 4
}

J ["li"]-j. meng // The property li in json j minus the property meng in json j. The result is-1.

"10" + "10" // addition can be used to concatenate strings. The result is 100.

// 2. Some short operators are defined in JavaScript.

Var num = 0 // define a number

Num ++; // indicates auto-increment, num = num + 1;
Num --; // indicates auto-subtraction, which is equivalent to num = num-1;
Num + = 2; // indicates auto-increment 2, which is equivalent to num = num + 2;
Num * = 8; // indicates 8, which is equivalent to num = num * 8;

// 3. Operator judgment

Var a = 1, B = 2; // an equal sign represents replication, and two variables are separated by ",", indicating simultaneous declaration.

A = B; // The result is false. Does it mean that a and B are equal?
A! = B; // is the result true, which means that a and B are not equal?
A <B; // does the result mean that a is smaller than B?
A <= B; // if the result is true, does a mean that a is less than or equal to B?
A> B; // if the result is false, does a mean that a is greater than B?
A> = B; // if the result is false, does a mean that a is greater than or equal to B?
"Two" = "three"; // true "tw" the index in the alphabet is greater than "th"
False> (a> B) // The result is true, which means comparison between false and false.

// 4. logical operators

(A = 2) & (B = 3) // The result is true. Is a equal to 2 and B equal to 3. & Meaning of and
A> 2 | B> 2 // The result is true. The first value is false, and the second value is true. | indicates or.
! (A = B) // The result is true .! Is reverse.


In the operator, only calculating a value does not affect any operation, but does not change the running status of the program. The statement does not contain a value, but changes the running status. The statement changes the running status, so it is followed by a plus sign.

Each function has its own name. You can call and execute a function by name, which can be defined once and called multiple times. The following is a simple example of a function.

The Code is as follows:


// 1. A function is a JavaScript code with parameters. It can be defined at a time, called multiple times, or with parameters.

Var a = 3; // declare a variable a with a value of 3;

Function fn1 (n) {// a function whose parameter is n named fn1
Return n + 1; // return a value greater than the input value.
}

Fn1 (a) // The result is 4. Because the declared value of a is 3, when calling a function, execute a + 1, that is, 3 + 1.

Var fOne = function (m) {// The function is also a data type, so you can assign a variable to a function.
Return m * m; // return a value for parameter * Calculation
}

FOne (a) // The result is 9

// 2. assign a function to the attributes of a variable.

Var arr = []; // create a new array
Arr. push (, 3); // use the push () method to add elements to the arr array from the back.
Arr. reverse (); // use the reverse () method to reverse the order of elements in the array

Var points = [// declare a json array in an element
{A: 0, B: 0 },
{A: 1, B: 1}
]
Points. dist = function () {// define a method for calculating the distance between two points in the declared Array

Var p1 = this [0]; // use this to obtain the reference of the current array.
Var p2 = this [1]; // and assign two new variables
Var a = p2.a-p1.a; // distance on the X axis
Var B = p2. B-p1. B; // distance on the Y axis

Return Math. sqrt (a * a + B * B) // use sqrt () in Math () to calculate the square root of two points.

}

Alert (points. dist () // result 1.414

// 3. Control statement
// Conditional statements and cyclic statements are called control statements.

Function abs (m) {// returns the absolute value function.

If (m> = 0) {// if the comparison result is true
Return m; // return m
} Else {// If the comparison result is false
Return-m; // return-m
}

}

Function factorial (n) {// calculate the factorial function

Var num = 1; // declare a variable with a value of 1

While (n> 1) {// when the expression in () is true, execute the code in the loop {}

Num * = n; // equivalent to num = num * n
N --; // equivalent to n = n-1

}

Return num // return the factorial result

}

Factorial (4) // The result is 24

Function factorialFor (n) {// use the for loop to implement factorial
Var I, num = 1; // declare variable I, and declare the variable num with a value of 1

For (I = 2; I <= n; I ++) {// cycles I from 2 to n
Num * = I; // The cyclic body. It can be omitted if there is only one sentence in the cyclic body {}
}

Return num; // return the calculated factorial table

}

FactorialFor (5)


From the function example, whether it is a while loop or a for loop, whether it is a judgment statement or a loop statement, it can be regarded as a control statement. Use certain conditions to control what will happen.

After introducing the functions, we will briefly introduce object-oriented functions.

The Code is as follows:


// Define a constructor, that is, first create an initialized object

Function Point (x, y) {// The first letter of the constructor name must be capitalized.
This. x = x; // this indicates the initialization object.
This. y = y; // Save the function parameters to the attributes of the initialization object.
} // The constructor does not need to return anything.

// Use the new Keyword and constructor to create a new object
Var p = new Point (); // create a Point with the plane coordinate ()

// Assign a value to the constructor prototype to add a method to the newly created object of the Point.
Point. prototype. r = function (){

Return Math. sqrt (
This. x * this. x + this. y * this. y
); // Use the sqrt () method in Math to calculate the square root. This refers to the object that calls the method.

}

P. r () // The result is 1.414.


The above example shows how to define a vertex with the square root method. JavaScript Object-oriented has some differences with other programming languages. What is the specific difference? We can only continue to study it.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.