Variables and inputs and outputs in JS

Source: Internet
Author: User

first, the use of three ways JS1, in the HTML tag, directly embedded JS (do not promote the use of)<button onclick= "alert (' point you ')" > Point me </button>>>> does not meet the requirements for separation of content and behavior2. Use <script></script> package JS code in HTML page<script type= "Text/javascript" >//JS Code</script> >>> Script tags can be placed anywhere on the page   3, the introduction of external JS file : <script language= "JavaScript" src= "js/01.js" ></script>   [Precautions]①<script></script> can be embedded anywhere in the page. However, the difference in location can lead to the execution of JS codedifferent orderFor example,:<script></script> put in front of <body>, then the JS code will be executed before the page loads   ② The introduction of external JS code,<script></script> must be paired with the appearance of the label. Moreover, the label can no longer haveany of the JS code/** Multiple lines of comments in JS. ctrl+shift+/ */ Single-line comment in//JS. ctrl+/ Second, JS in the variable1, the wording of the variable declaration in JSvar num=10;//variables declared with Var, which are local variables and valid only within the current scopenum=10;//variables declared without Var, the default is the global variable, in the entire JS file is validvar x=9,y,z=10;//uses a single line of statements to declare multiple variables at the same time. In the previous style, Y is declared, but notassignment status, the result is undefined [Considerations for declaring variables] all variables in the ①JS are declared using the var keyword. The variable is exactly what number type, depending on the changetype of the value assignment      The same variable in ②js, you can modify the data type of the variable at multiple different assignmentsvar a=10;//from the initial declaration, a belongs to the integer type;a= "hahaha";//The integer type A is modified to be a string type when the value is repeatedly assigned   the ③ variable can be declared with Var, or it can be used without the Var declaration. [difference] is declared as a local variable using VAR,declare as a global variable without using var④ is only declared with Var, but not assigned, and the result is undefinedFor example: Var a;//a is undefinedHowever, if you do not declare or assign a value to a, the direct use will be an error      ⑤ the same variable name can use the Var declaration multiple times. However, the latter var is of no use;var declaration, it will only be understood as normal 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 (Camel name law):The first letter is lowercase, then the first letter of each word is capitalized;Mynameis② or use Hungarian nomenclature:all letters are lowercase, and the words are separated by _my_name_is③mynameis can be used, but not standardized4, JS data type: Undefined: Not defined . Variable declared with Var, but not assigned null: Represents an empty reference Boolean: Boolean type . true and False, only two values: True/flase Number: Numeric type . Can be an integer, or it can be a decimal String : Type . Use "" or " wrapped content to become a string object: Types of objects, subsequent explanations ... 5. "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 using the number () function if the final result canconverted to a number, it is not nan and the result is false    ② Number () function : attempts to convert other types of data to numeric; [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 characters and cannot be turned; "1a"->nan [Boolean type] true->1 false->0 [Null/undefind] null->0 Undefind->nan [Object] * (Follow-up explanation)      ③ parseint (): Converting 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 functions can go to various data types, the parseint function only goes to the string2. The results are not exactly the same when you turn the strings. (see above for explanation)     ④ parsefloat: Converts a string to a character 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,is still an integer12.5 "->12.5; "->12 "      ⑤ typeof: The data type of the detection variable string->string true/false->boolen numeric->number->undefined Object/null->object function not defined->functionThree, JS commonly used in the input and output statements1,document.write (); the contents of () are printed and output to the browser screen; Note: All content except variables/constants must be placed in "". Variables and constants must be placed outside theIf you have variables and strings at the same time, you must use the + link;Eg:document.write ("The card in the left hand:" +left+ "<br/>2,alert (); Using the bullet box output pop-up Warning, () the content is the same as the above requirements3,prompt (); Bullet box input receive two-part parameters:① The input box above the prompt content, optional;② The default information in the input box, optional;indicates the prompt above the input box when only part of the text 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 and the variable will be assigned a value of NULL        When you enter content, the data type that is received by default is a string!!!

Variables and inputs and outputs in JS

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.