JavaScript------Basic use of syntax (variables, operators, statements)

Source: Internet
Author: User

JS (JavaScript)

first, the history story of JavaScript

1. The origin of JavaScript language and name (Netscape,Sun Java)

2. Microsoft's Explorer and JScript


second, JavaScript overview

< Span style= "White-space:pre" > JavaScript a literal-translation scripting language is a dynamic type, weak type, prototype-based language with built-in support types. Its interpreter browser clients Span style= "FONT-FAMILY:ARIAL,SANS-SERIF; line-height:24px; text-indent:28px "> scripting

words, earliest is used in HTML (an application under standard Universal Markup Language) to add dynamic functionality to HTML pages.

JavaScript is an object-and event - driven scripting language that is primarily applied to the client.

Characteristics:

1. Interactivity (it can do is the dynamic interaction of information)

2. Security (does not allow direct access to local hard disks)

Cross-platform (as long as the browser can interpret Js can be executed, and platform-independent)


c. JavaScript differs from Java

1. JS is Netscape Company's products, formerly known as LiveScript; Java is a product of Sun and is now a product of Oracle Corporation.

2. JS is Object-based andJava is Object-oriented.

3. JS can be executed simply by explanation,andJava needs to be compiled into a bytecode file before execution.

JS is a weak type andJava is strongly typed.


iv. JavaScript Syntax

Each language has its own grammar rules,JS syntax and Java is very similar, so it is easier to learn. JS also has the same variables, statements, functions, arrays and other common language elements.

1. Variables

Defined by the keyword var , a weak type is not specified without specifying a specific data type.

Example:var x = 3; x = "Hello";

Note:JS Special constant value:undefined, when the variable is not initialized to be used, the value of the variable is undefined(undefined).

Note:Javascript statements at the end of the sentence can be terminated without semicolons, the characteristics of non-rigorous language.

But in order to conform to the programming specification, you need to define the terminator like Java .

And there are cases where a semicolon must be written, such asvar x = 3 ; var y =5 If two statements are written on the same line, a semicolon is required to separate them.

1) Keywords: almost like Java

2) identifier, delimiter: Same as Java

3) Note: These two types of Java are used://and/*/

4) Data type: Number type, String type, Boolean type, undefined (when variable is declared but not assigned)

5) Variable: var (weak type, similar to object in Java)

6) in JS, the single and double quotation marks are the same, the package is a string (but also has two quotation marks, inside the single quotation mark)


2. Operators

The operators in Javascript are roughly the same as Java .

Just a few points to note during the operation:

1) var x = 3120/1000*1000; x = 3120; and not the .

2) var x = 2.4+3.6; x = 6; and not 6.0

3) var x = "12" + 1; x = "121"; x = "12" –1; x = 11;

A plus sign is a connector for a string

4) && | | is a logical operator & | is a bitwise operator.

5) also supports ternary operators

6) Special operator typeof: A string that returns the data type of an action expression.

Code Demo:
<script type= "Text/javascript" >//2 operator//2.1:+ (positive),-(negative), + (plus),-(minus), *,/(except),% (remainder), + + 、--a=12340; Alert ("a=" + a/1000); 12.34 JS there is no int and float, there is only one type of number//alert ("a=" + a/1000*1000); 12340//alert (12+1); //alert (' 12 ' + 1); 121//alert (' 12 '-1); //alert (' AB '-1); Nan:not A number is not a value, here is the meaning of "illegal"//alert ("12AD"-1);        NaN://alert (True); alert (true+1);//Results: 2, because the default value of True is: 1//alert (false+1); Results: 1, because the default value for False is: 0//alert (false+true);//Result: 1, because the "+" number is an arithmetic operation, not a logical operation//alert (false| |        true);//Result: True, because this is the logical operation if (true==1) {//true is automatically upgraded to number type, go and 1 to compare//alert ("jjkkj"); }//alert (100%3); Results: 1//alert ( -100%3);        Results: -1//i++ and ++i, identical to the usage in Java, var n=3,m;        m=n++; Alert ("m=" +m + ", n=" +n);        Results: m=3,n=4 var x=3,y;        Y=++x; Alert ("y=" +y + ", x=" +x); Results: x=4,y=4//2.2 Assignment operators: =, + =,-=, *=,/=,%=, etc. x = 123;        x%=2;                Alert ("x=" +x);        2.3 Relational operators: = =,! =, >, <, >=, <= x = 3; if (x!=2) {//Result: True//alert (x);//3}//2.4 logical operator:& (with), | (OR),! (non), ∧ (XOR), && (Condition and), | |        (condition or) var t = 4;        Alert (t>3 && t<10); Alert (! (                T&GT;3)); JS and C language, there are "0" and "non-0" concept, "0" stands for false, others are represented by true t=3;        Is "Non 0"---True//t=-1;//is "not 0"---true if (t) {//alert ("ooo"); } t=4; Is "Non 0"---true//alert (!t);//Result: false//2.5-bit operator: ~ (non), & (with), | (or), ^ (XOR), << (left shift), >> (right Shift ), >>> (unsigned right shift) Var c=6; The complement of 6 is stored://alert (c&3); Results: 2//alert (5^8^8); Either a number, and a certain number of different or two is itself. Here is a common code technology: the background erase when drawing is commonly used this function//alert (C&GT;&GT;2);       Results: 1 c=-6; alert (c>>2);//results: -2, ">>" moves right 2 bits in the form of complement, and 2 bits "sign bit on the left, here is 1"---sign bit or 1 invariant//alert (C&GT;&GT;&GT;2);Results: 1073741822 ">>>" shifted right 2 in the form of complement, and 0-----on the left, and the sign bit was shifted to the right, so the sign bit of the new number was 0 alert (3>0?100:200); The sign expression is the same as Java </script>

3. Statements(with theJavaStatement Format is the same)

1) Judging the structure(IfStatement)

Note:var x = 3;

if (x==4)// can be compared operation.

if (x=4)//can be assigned to the operation, but also can be judged. No error.

Because in Js 0 or null is false,

Not 0 or non- null is true(usually in 1 ).

So the if (x=4) result is true;

The problem can be resolved through if (4==y) . Because the 4=y will not judge, but will error.

2) Select structure(SwitchStatement)

A, switch-case matching order: case is from top to bottom matching, the default item either before or after the last match.

B, switch-case in Java can only match: Byte,int integer type, char type, and jdk1.7 new string type. The switch-case in JS can match any type (unlike Java : A string can also be selected because of a weak type). )

3) Cycle structure( whilestatement,Do...whilestatement, forStatement).

Note: The difference is that there is no specific data type restrictions, use should be noted. JS in the way output to the page: document.write ("text (also can write HTML language)");

Example 1:
<!--break,continue return used in function--><script type= "text/javascript" ><span style= "White-space:pre" > </span>document.write ("

Example 2:99 multiplication table on page output

<! DOCTYPE html>

Results:








Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

JavaScript------Basic use of syntax (variables, operators, statements)

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.