JavaScript language core data types and Variable usage Introduction _ Basics

Source: Internet
Author: User
Tags access properties logical operators null null numeric value script tag square root

Any programming language has its own language core, and like many programming languages, JavaScript has its own language core. The core of language is generally called the most fundamental part of JavaScript. What is called the beginning of everything is difficult, learning JavaScript is also a truth, there is an old saying well, good beginning is half of success. The core language of learning and learning JavaScript is a very good start on the JavaScript study path.

Here's a piece of code that focuses on the data types and variables of javascript:

Copy Code code as follows:

In the script tag, all//The following is called a comment
The function of annotations is generally to explain some javascript, so that other Web front-end development engineers or read this code more clearly

A variable is a symbolic name of a value, and by the name of these variables, we can generally know what the variable is and what variable type it is.
Distinguishing variables is simple. Variables are preceded by "Var", which means that the variable is declared through "var".

var m; Declare a variable m

Generally we assign the value to be declared to the variable with the equal sign
var m = 10; Now the variable m equals 10

m//through the variable m to get the value just declared

Alert (m)//using the alert () function to eject the value of M in the browser

Data types for javascript: boolean, numeric, string, Underfind, function, array, object

var n = 1; Digital
n = 0.01//integer and real numbers are numeric types

var s = "Hello"; A string consisting of text enclosed in double quotes
s = ' national security '; A string consisting of text enclosed in single quotes

var B = false; The Boolean value of the error
B = true; The correct boolean value

var z = null; Null null value, is a special type, typeof is the object

var u; Underfind

var j = {//one object representing JSON
Li:3,//property "Li" is a value of 3
The value of the Meng:4//property "Meng" is 4
}

J["Li"]//access to values in JSON via []
J.li//through. Accessing values in JSON
J.long = 5//Creates a new property by means of an assignment
J.ai = {//You can create a new JSON by assigning a value
Xin:33
}
J.kong = {}//{} represents an empty object, it has no attributes

J.ai.xin//Pass. Access properties in new JSON

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

A[0]//Array [] represents the position of the number in [], the array starts at 0, so A[0] is the first element of the array
A.length//length represents the number of array A, 3
A[A.LENGTH-1]//Represents the last element in an array
A[9] = 2; To add a new element with a method of assignment

If a = [], then it represents 0 elements in the array, a.length = 0

A[0] = {
li:333
}

JSON can contain arrays, and can contain JSON in the array

Alert (a[0]["Li"])

In the code above, you can pass "[]", "{}", "." Define the object, or you can pass the [], ' {} ', '. ' Change the contents of an array or object. You can also pass "[]", "{}", "." To read some of the data in the object. The following set of codes is for operators:
Copy Code code as follows:

In JavaScript, you can use a budget character to perform an operation between two numbers to produce a new value.
The following are some of the more common budget characters, such as "+", "-", "*", "/"

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

var j = {//one object representing JSON
Li:3,//property "Li" is a value of 3
The value of the Meng:4//property "Meng" is 4
}

J["Li"]-J.meng//json J Property Li minus the properties Meng in JSON J, the result is-1

"10" + "10"//addition can be string stitching, the result is 100

2. Some shorthand operators are defined in JavaScript

var num = 0//define a number

num++; On behalf of the self increase, num = num + 1;
num--; Represents a self reduction, equivalent to num = num-1;
num + + 2; Represents an increase of 2, which is equivalent to num = num + 2;
Num *= 8; The representative claimed to be 8, equivalent to num = num * 8;

3, operator to judge

var a = 1,b = 2; An equal sign is a copy, separated by a "," between two variables, representing the simultaneous declaration

A = = B; The result is false, meaning a and B are equal?
a!= b; The result is true, does it mean that A and B are not equal?
a < b; The result is true does it mean A is less than B?
a <= b; The result is true meaning is a less than or equal to B?
a > B; The result is false, which means a greater than B?
a >= b; Is the result false meaning a greater than or equal to B?
"Two" = = "three"; True ' TW ' index in the alphabet is greater than ' th '
False > (a > B)//The result is true meaning is false and false for comparison

4. Logical operators

(A = = 2) && (b = = 3)//The result is true. A is equal to 2 and B is equal to 3. && representatives and meaning
A > 2 | | B > 2//Results True, the first is false, and the second is true, because | | The meaning of a representative or
! (A = = B)//The result is true.! It means to be reversed.

operator, only the calculation of a value does not affect any operation is called an expression, does not change the running state of the program. The statement does not contain a value, but it changes the state of the operation. The statement is followed by a semicolon, because it changes the running state.

Each function has its own name, which can be called by name to perform a function that can be defined once and called multiple times. Here is a simple example of a function.
Copy Code code as follows:

1, the function is a section of JavaScript code with parameters, can be defined once, multiple calls, can also have parameters

var a = 3; Declare a variable A with a value of 3;

function fn1 (n) {///an argument is n-named fn1
return n+1; Returns a value that is greater than the value passed in
}

FN1 (a)//result is 4, because the value of a just declared a is 3, so call function, execute a+1 namely 3+1

var fone = function (m) {//functions are also a data type, so you can also assign variables to a function
return m*m; Returns a numeric value for the operation of the parameter * parameter
}

Fone (a)//the result is 9

2, the method, the function assigned to the variable properties

var arr = []; Create a new array
Arr.push (1,2,3); Add an element from the back to the ARR array using the push () method
Arr.reverse (); Reverse the order of the elements in an array with the reverse () method

var points = [//Declare an array of JSON 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 get a reference to the current array
var p2 = this[1]; and assign a value to two new variables.
var a = p2.a-p1.a; The distance on the x axis
var B = p2.b-p1.b; Distance on y-axis

Return math.sqrt (a*a + b*b)//() calculates the square root by using sqrt () in Math () to get the distance between two points

}

Alert (Points.dist ())//result is 1.414

3, control statements
Conditional statements and circular statements are called control statements

Function abs (m) {//Find absolute value functions

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

}

functions factorial (n) {//function to compute factorial

var num = 1; Declare a variable with a value of 1

while (n > 1) {//when the expression in () is true, the code inside the Execute loop {}

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

}

Return num//Returns the result of factorial

}

Factorial (4)//The result is 24

function Factorialfor (n) {//factorial with A for loop
var i, num = 1; Declaring the variable i and declaring a variable num with a value of 1

For (i=2 i <= n; i++) {//I loop from 2 to n
Num *= i; Loop body, when only one sentence in the circulation body can be omitted {}
}

return num; Returns the calculated factorial table.

}

Factorialfor (5)

As you can see from the function example, whether a while loop or a for loop, whether it is a judgment statement or a loop statement, is a control statement. Control what is going to happen through certain conditions.

After introducing the function, let's introduce a brief introduction to object-oriented.
Copy Code code as follows:

Defining a constructor is to create an initialized object first

function Point (x,y) {//constructor name first letter to capitalize
this.x = x; This represents the initialization object
This.y = y; To deposit the parameters of a function into the properties of this initialization object
}//constructor does not need to be returned, return something

Create a new object using the New keyword, and the constructor
var p = new Point (1,1); Create a point with a planar coordinate (1,1)

Add a method to the newly created object for point by assigning the constructor prototype
POINT.PROTOTYPE.R = function () {

Return Math.sqrt (
This.x*this.x + this.y*this.y
); The sqrt () method in math is used to open the square root operation. This refers to the object that invokes the method

}

P.R ()//The result is 1.414

The example above is to teach you how to define a point that has a square root method. There are some differences between JavaScript-oriented objects and other programming languages. The specific difference, we can only continue to study, in order to know.

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.