JavaScript base One (JS basic functions and operators)

Source: Internet
Author: User
Tags bitwise operators logical operators object object

"Three ways to use JS"
1, in the HTML tag, directly embedded JS (do not promote the use of)
<button onclick= "alert (' point on Point ')" > Point me </button>
>>> does not meet the requirements for separation of content and behavior
2. Use <script></script> package JS code in HTML page
<script type= "Text/javascript" >
JS Code
</script>
"<script></script> tag can be placed anywhere on the page
3, the introduction of external JS file:
<script language= "JavaScript" src= "Js/01js.js" ></script>
"Precautions"
①<script></script> tags can be embedded anywhere on the page, but the difference in location will cause the JS code to be executed in different order;
For example,:<script></script> placed in front of <body></body>, then the JS code will be executed before the page load;
② the introduction of external JS code,<script></script> must be paired with the appearance of the label, and the label can no longer have any JS code.

"Variables in JS"
1, JS in the wording of the variable declaration:
VAR num=10;//variables declared with Var, which belong to local variables and are valid only within the current scope;
num=10;//variables declared without Var, belong to the default global variables, the whole JS file is valid;
var x=8,y,z=10;//uses a single line of statements to declare multiple variables at the same time. In the above formula, y belongs to the declaration, but not the assigned state, the result is undefined;
"Considerations for declaring variables"
The declaration of all variables in ①js uses the var keyword, which is the data type of the variable, depending on the type of assignment to the variable;
②js the same variable, you can modify the data type of the variable in several different assignments:
var a=10;//from the initial declaration, a belongs to the integer type;
A= "haha"///duplicate assignment, an integer type a becomes a string type;
The ③ variable can be declared with Var, or it can be used without
The "difference" using the Var declaration is a local variable, not used as a global variable;
④ is only declared with Var, but not assigned, and the result is undefined:
For example: Var A;,, A is undefined
However, if you do not declare or assign a value of a, direct use will be an error;
⑤ agrees that variable names can be declared using var multiple times, but the latter var is useless.
The second time a VAR declaration is used, it is only understood as a normal assignment operation.
2. Naming requirements for variable names
① variable names can only be composed of letters, numbers, underscores
② cannot start with a number.
③ variable names are case-sensitive, and uppercase and lowercase letters are different variables.
3. Naming conventions for variable names
① to conform to the small hump rule (Camel name law);
First letter lowercase, followed by the first letter of each word capitalized
② or use Hungarian nomenclature:
All letters lowercase, with underscores between words-separate

"Data type in JS"
①undefined does not define a variable that has been declared with Var, but does not have an assignment such as Var A;
②null represents an empty reference
The ③booleam Boolean type represents true and false, with only two values: "True/flase
④number numeric type can be a positive number, or it can be a decimal
⑤string string type with "" or "wrapped content, called string
⑥object Object Type

"Common numeric Functions"
①isnan (): Determines whether a variable or constant is Nan (not a num non-numeric)
When you use isNaN (), you try to convert by using the number () function, and if the final result is converted to numbers,
is not nan and the result is false.
②number () function: Attempts to convert another type of data to a numeric type.
"String Type"
"" String is a literal numeric character, which will be converted to the corresponding number: "111" = "111
"" String is an empty string and will be converted to 0: "" = "0
string contains any other characters, it cannot go: "1a" = "NaN
"Booleam"
"True" = "1
"Flase" = "0
"Null/undefined"
Null= "0 undefined=" NaN
"Object" follow-up explanation
③parseint (): Converting a string to an integer type
"" The pure numeric string, can turn: "12" = "12", "12.9" = "12" (decimal conversion, directly erase the decimal point, not rounding)
"" Empty string cannot be transferred
", which contains strings of other characters, will intercept the number part before the first non-numeric string;
"123aa234" = "123" a123v123 "=" NaN
"Parsint function () can only be transferred to the string, to other types, all Nan
"Number function differs from parseint function"
1, number function can go to various data types, Parsint function can only go to string type
2. The results are not exactly the same when the two strings are turned.
④parsefloat () function: Converting a string to a numeric type
The conversion rule is the same as parsint, except that if there are decimals, the decimal point is preserved, and if there are no decimals, it remains an integer.

⑤TYPEPF: The data type of the detection variable
String = "String value =" Number True/false= "Boolean
undefined = "Undefined objects/null=" object function = "functions

"Input and output statements commonly used in JS"
1, document. Write (); Print the contents of the parentheses to the browser screen;
Note that all content except variables, constants, must be placed in "", variables and constants must be placed outside the "";
If you have variables and strings at the same time, you must use the + link;
Eg:document.write ("Solitaire in the left hand" +left+ "<br/>")
2. alert (); Use pop-up window output;
Pop-up Window warning, () the contents of the same as above;
3, prompt (); pop-up window input
Receive two-part parameters:
① the input box above the prompt content, optional;
② The default information in the input box, optional;
Indicates the contents of the input box when only part is written;
You can define variables to accept the input content. Click the OK button and the variable will be assigned to the input content.
Click the Cancel button and the variable will be assigned a value of NULL.
When you enter content, the data type that is accepted by default is a string!!!!

"Operators in JS"

1. Arithmetic operation (single-mesh operator)
+ Plus, minus, * multiply,/divide,% withdraw, + + self-increment 、--self-reduction
+: There are two kinds of functions: connection string/addition operation;
When the + both sides are full of numbers, the addition operation is performed;
When the + side has either side as a string, the function of the connection string, the result of the link is a string;
In addition to +, the rest of the symbol operation will first try to convert the left and right variables with the number function to the numbers
/: The result will retain the decimal point
+ +: increment operator, the variable is based on the original +1
--: The decrement operator, the variable on the original basis-1

① the same point: either a++ or a--, the value of a will be +1 after the operation is completed.
Different points: a++, first use the value of a to calculate, and then put A+1
a--, first put a+1, and then use the value of a+1 to calculate

2. Assignment operation
= Assignment (assigns the right value to the left) + = = *=/=%=
+=:a+=b is equivalent to a =a+b, but the former is faster than the latter, and the former is recommended;

3. Relational operations
= = equals, = = = Congruent,! = Unequal,! = = Not congruent, >, <, >=, <=
>>> relational operators, the result after operation, can only be a Boolean type;
>>> to determine if a number is in a certain range, you must use && link;
a<10&&a>0√10>a>0x
>>>===: Strictly equals: Requires not only the same type, the value must be the same, the type is different, directly false, the same type, and then proceed to the next judgment
= = equals: The same type, with the same as = = =, the type is not the same, will first try to use number () to convert the two sides into numbers, and then to judge.
But there are individual exceptions, such as: NULL==FALSEXNULL==UNDEF√NULL==NULLX

4. Conditional operator (multi-mesh operation)
A>b?true:false
There are two important symbols:? And:
When? The preceding section, when the result of the operation is true, executes: the preceding code
When? The preceding section, when the result of the operation is false, executes: The following code
A value can be used on either side of the colon, and the entire formula can be assigned. var a=1<2?1:2
Both sides of the colon can be code blocks, and code is executed directly. 1<2?alert (1): Alert (2)
Multi-mesh operators can be nested in multiple layers. var A=1<2\?alert (1):(1>0?4:5)

5, bitwise operators, logical operators
&, |, ~ && and, | | Or! Non -
&&: Both sides are set, the result is true
| |: On either side of the set, the result is true

6. Precedence of Operators
() parentheses Highest
! ++ --
* / %
+ -
< > >= <=
== !=
&&
||
= + = = * =/= the lowest is a variety of assignment operators

JavaScript base One (JS basic functions and operators)

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.