Getting Started with JavaScript (i)

Source: Internet
Author: User
Tags script tag

First, what is called JS?

  1, the concept of JS
JS is the abbreviated form of JavaScript, and JavaScript is a client-side scripting language that is Object-and event-driven and has relative security. In web development, JS occupies a pivotal position, all the interaction, logic layer of code should be implemented by it.

  2, JS play a role
If the site to build a metaphor for building, then HTML is the building of the steel cement, CSS is the layout of the building decoration, and JS is a large and small buildings in the ubiquitous switch. From this point of view, JS is the web development in charge of the logic layer of the language, and today is quite hot "user experience" concept, the most important part of the code is still need to write JS.

  3, three ways to use/reference JS

① in HTML tags, directly embedded JS (do not advocate the use of):
Eg:<button onclick= "alert (' You really point it!! ' > I'm!!!. </button>
>>> does not conform to the requirements of content and behavior separation!!!

The code is as follows:

The results of the operation are as follows:

  

② using <script></script> wrapping JS code in HTML page; '
eg
<script type= "Text/javascript" >
JS Code
</script>
The >>>script tag can be placed anywhere on the page.

③ introduction of external JS files:
Eg:<script language= "JavaScript" src= "js/01-class notes. js" ></script>

  [Precautions]

①<script></script> can be embedded anywhere in the page, but the difference in location causes the JS code to be executed in a different order.
For example:<script></script> put in front of <body>, then the JS code will be executed before the page load;
② the introduction of external JS code,<script></script> must be paired to appear in the label, and the label can no longer have any JS code.

Second, JS in the variable  

  1, the wording of the variable declaration in JS

    var num = 10; Variables declared with Var, which are local variables, are valid only at the current scope;
num = 10; Variables declared without Var, the default is a global variable, in the entire JS file is valid;
var x=8,y,z=10; Use a single line of statements and declare multiple variables at the same time. In the above formula, Y is declared, but is unassigned, and the result is undefined (defined, not assigned);
    [Considerations for declaring variables]
The declaration of all variables in ①js uses 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 = 10; From the initial declaration, a belongs to the integer type;
A = "Hahaah"//Duplicate assignment, the integer type A is modified to the string type;
The ③ variable can be declared with Var, or it can be used without the Var declaration.
[difference] using VAR declared as a local variable, not using Var declared as a global variable;
④ is only declared with Var, but is not assigned a value. The result is undefined, but if you do not declare or assign a value to a, the direct use will give an error;
For example: Var A; A is undefined.
⑤ the same variable name, you can use the Var declaration multiple times. However, there is no egg in the back of Var. The second time a VAR declaration is used, it is only understood as a normal assignment operation.

  2. The naming requirements of variable names in JS

① variable names can only be composed of letters, numbers and underscores;
② the beginning cannot be a number;
③ variable names are case-sensitive, uppercase and lowercase letters are different variables;

  3, the naming specification of variable names in JS

① to conform to the small hump rule (camel nomenclature): The first letter in lowercase, followed by the first letter of each word capitalized;
Eg:mynameiszhangsan√
② or use Hungarian nomenclature: All letters are lowercase and the words are separated by _;
Eg:my_name_is_zhang_san√
③mynameiszhangsanx can be used, but not standardized.

4.data type in JS (emphasis)
①undefined: Not defined. Variables declared with VAR have been used, but no value has been assigned. var A;
②null: Represents an empty reference.
③boolean: Boolean type. Show true, False, only two values: true/flase;
④number: Numeric type. Can be an integer, or it can be a decimal;
⑤string: String type. With "" or "wrapped content, called a string;
⑥object: Object type;

5. [Common numerical functions](emphasis)
①isnan (): Determines whether a variable or constant is Nan (not a num non-numeric); When judged using IsNaN (), it attempts to convert using the number () function, which is not nan if the final result can be converted to a digit, and the result is false;
The ②number () function, which attempts to convert other types of data to numeric type;
[String Type]
>>> string is a pure numeric string and 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 when it cannot be transferred; "1a"->nan
[Boolean type]
>>>true->1 flase->0;\
[Null/undefined]
Null->0; Undefined->nan

③parseint (): Converts a string to an integer type;
>>> Pure numeric string, can go.
"->12;" "12.9"->12 (decimal conversion, directly erase the decimal point, not rounding)
>>> empty string, cannot go. ""->nan
>>> A string containing other characters that will intercept the number part before the first non-numeric string;
"123a456"->123; "A123b456"->nan
>>>parseint () 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, except that if there are decimals, the decimal point is preserved, and if there are no decimals, it remains an integer;
Eg: "12.5", "12.5" "->12"
⑤typeof (): Detects the data type of the variable:
String->string numeric->number undefined->undefined
True/false->boolean function/null->function Object/null->object

Three, JS commonly used in the input and output statements

1, document.write ()

Print out the contents of () to the browser screen, and be aware that all content except variables/constants must be placed in the. Variables and constants must be placed outside the "", and if there are both variables and strings, a + link must be used;
Eg:document.write ("The card in the left hand" +left+ "</br>");
2. alert (); Use pop-up window output
The contents of the pop-up Window warning, () are the same as the above requirements.

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;
When you write only a part, it indicates the content of the prompt above the input box; You can define variables and receive input. 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 the content, the data type that is received by default is a string!!!!!!!
Code Listing 1:

    

Operation Result:

    

Code Listing 2:

    

Operation Result:

        

Getting Started with JavaScript (i)

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.