JS Basics-Variables and the use of input and output and operators

Source: Internet
Author: User
Tags bitwise operators logical operators script tag

First, what is JavaScript?

Before we talk about the basis of JS, let us know what is JS Bar.

JS, full name JavaScript. is a literal translation scripting language, a dynamic type, a weak type, a prototype-based language, and a built-in support type. Its interpreter, known as the JavaScript engine, is widely used as a scripting language for the client, and is used in HTML (an application under the standard Universal Markup Language) to add dynamic functionality to an HTML Web page.

Second, the use of JS three ways

1, in the HTML tag directly embedded JS (): Do not advocate the use of.
  

<onclick= "alert (' Little hooker hit! You're a bit of a man! '> have the ability to point me!! </ Button >

>>> does not conform to the requirements of the content and behavior separation!!!


2. Use the <script></script> package JS code in the HTML page:

  <type= "Text/javascript">  //js Code;  </script>

The >>>script tag can be placed anywhere on the page.

3, the introduction of external JS files, using <script></script> tags:

<language= "JavaScript"  src= "Js/01.js"></ Script >

[Precautions]
①<script></script> can be embedded anywhere in the page. However, the difference in location causes the JS code to be executed in a different order
For example <script></script> in front of <body>, the JS code will be executed before the page is loaded
② the introduction of external JS code,<script></script> must be piles of tags. Also, there can be no code in the tag.

C. JS variables

1, JS in the wording of the variable declaration;
var num = 10; Variables declared with Var, which are local variables, are valid only within the current scope.
num = 10; Variables declared without VAR, default to global variables, are valid throughout the JS file.
var = x=8,y,z=10; Declare multiple variables at the same time using a single line statement. In the above formula, y belongs to the declared but not assigned state, and the result is undefined;

[Considerations for declaring variables]
All variables in the ①JS are declared using the var keyword. The variable is exactly what data type, depending on the type of assignment to the variable;
②js the same variable, you can change the data type of the variable at multiple different assignments;
  var a = ten; From the initial declaration, is a belongs to integer type;
  a = "hahaha"; An integer type A is modified to be a string type when the assignment is repeated;
The ③ variable can be declared using VAR or var declaration.

[difference] using VAR declared as a local variable, not practical Var declared as a global variable;

④ is only declared with Var, but not assigned, 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 give an error.
⑤ the same variable name can use the Var declaration multiple times. However, the trailing Var does not have any ova. The second time the Var declaration is used, it is only understood as a normal assignment operation.

2. Naming requirements for variable names:
① variable name, can only have letters, numbers, underline composition;
② the beginning cannot be a number;
③ variable names are case-sensitive, uppercase and lowercase letters are different variables;

3. Naming conventions for variable names:
① to conform to the small hump rule:
The first letter is lowercase, then the first letter of each word is capitalized;var mynameissong = 1; ?
② or use Hungarian nomenclature:
All letters are lowercase, separated by _;var my_name_is_song = 1; ?
③var mynameissong = 1; ?, can be used, but not canonical.
  

4, the data type in JS

①undefined: Not defined. Variables declared with VAR have been used, but no value has been assigned. For example,var A;
②null: Represents an empty reference.
③boolean: Boolean type. True and False, only two values: true/false
④number: Numeric type. Can be an integer, or it can be a decimal;
⑤string: String type. The content enclosed in double or single quotation marks is called a string;
⑥object: Object type.

5. Commonly used numerical functions
①isnan (): Determines whether a variable or constant is Nan (not a num non-numeric);
When judged using isNaN (), an attempt is made to convert using the number () function, and if the final result can be converted to a digit, it is not nan and the result is false.
②number () function: Attempts to convert other types of data into numerical form;

var num = 1; Alert (number (num));

[String type]
>>> string is a pure numeric string, which is converted to the corresponding number; "111"->111
>>> string is an empty string and will be converted to 0; ""->0
>>> string contains any other string, it cannot be turned; "1a"->nan
  
[Boolean type]
True-1; False--0

[null/undefined]
Null-0; Undefined-1

[Object]

③parseint (): Converts a string to an integer type;
>>> Pure numeric string, can go. "12"; "12.9", 12; (When fractional conversion, erase the decimal point directly, not rounding)
>>> empty string, cannot go, "", NaN
>>> a character nginx that contains other characters, which intercepts the number part before the first non-numeric string; "123a456", 123; NaN, "a123b456"
>>>passint () can only be transferred to a string, to other types, all Nan.

[number function differs from parseint function]
1, number function can be transferred to various data types, parseint function can only go to string.
2. The results are not exactly the same when the two strings are turned. (see above for explanation)
  
④parsefloat: Converts a string to a numeric type;
The conversion rule is the same as parseint, and the value is the decimal point if there are decimals: if there are no decimals, it remains a positive number
"12.5", 12.5; "12";
  
⑤typeof: The data type of the detection variable:
string-strings not defined-Undefined true/flase, Boolean
Value, Number object/null, function

Four, JS commonly used in the input and output statements

1, document.write (); Prints the contents of () to the browser screen;
Note: 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;

var num1 = prompt ("Please enter first number"), var num2 = prompt ("Please enter second number"), var sum  = parsefloat (NUM1) + parsefloat (num2) alert ("Result is" + SUM);

2, alert (): The use of pop-up window output;
The contents of the pop-up Window warning, () are the same as the above requirements.
3, PROMT (); Pop-up window input;
Accept 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 receive the input content. Click the OK button, the variable will be assigned to the input content, click the Cancel button, the variable will be assigned to NULL.
When you enter content, the data type that is received by default is a string!!!

V. Use of Operators

1. Arithmetic operation (single-mesh operator)
+ Plus, minus, * multiply,/divide,% withdraw, + + self-increment,--self-reduction
>>> +: There are two kinds of functions, link string/addition operation. When the + sides are all numbers, the addition operation is run;
When the + side has either side as a string, the function of the link string, the result of the link is a string.
In addition to +, the rest of the symbolic operation will first attempt to convert the left and right variables to numbers using the number function.
>>>/: The decimal point will be retained as a result.
>>> + +: increment operator, the variable is based on the original +1.
>>>--: The decrement operator, the variable on the original basis-1.

"The similarities and differences of a++ and ++a"
① the same point: whether it is a++ or ++a, after the operation, a value will be +1;
② different points: a++, first use the value of a to calculate, then the a+1;
++a, the first a+1, after using the value of a+1 to calculate;

<Scripttype= "Text/javascript">    vara= 3, B,c; b=a++ +2; //first with a operation, b=3+2 again a+1,a+4C= ++a+2; //first, the a+1,a=5, after using the value of a+1 to calculate, c5+2Alert ( a) alert (b) alert (c)</Script>

2. Assignment operation
= Assignment, + = = *=/=%=

+ =: a+=b; equivalent to a=a+b; however, the former is faster than the latter, so it is recommended to use + =;

The rest of the operators are similar.

3. Relational operations
= = equals, = = = Congruent (strictly equals),! = unequal,!== not congruent, >, <, >=, <=

>>> relational operator, result after operation, only Boolean type
>>> to determine if a number is in a certain range, you must use && link;
  a<10 && a>0;? 10>a>0;?
>>> = = = Strictly equal To, requires not only the same type, the same value must be the same, different types, the result is directly false, the same type, in the next judgment;
= =: equals. The same type as the = = = effect. When the type is different, the number function is used to convert both sides to numbers before judging.
But there are individual exceptions, such as: Null==false; ? null==undefined; ?

4. Conditional operator (multi-mesh operation)
A>b?true:false

There are two important symbols:? and :
When? When the preceding section evaluates to True, execute: The preceding code
When? When the previous operation evaluates to False, execute: 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:
()//parenthesis Highest
! , + +,--//Monocular operator
* 、 / 、 %
+,-
>, <, >=, <=
= =,! =
&&//With or simultaneous presence of,&& | | High
||
=, + =,-=, *=,/=//lowest are various assignments

JS Basics-Variables and the use of input and output 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.