JS Basics-Prerequisites for entry

Source: Internet
Author: User
Tags logical operators script tag

• First, to simply say, JS is what , JS is a shorthand for JavaScript, is

Browser-based Object-based Event-Driven scripting language· So what's the use of JS? TA can achieve: Form Validation Add an animation effect dynamically change page content Ajax Network Requests· So how do you use it in a Web page? There are three ways to do this:embedded in 1.Html<button onclick= "Javascript:alert (' young man. Cheat you ') ' > Dragon Slayer Blades click on Send! </button>2. Use JS directly in the HTML page<script type= "Text/javascript" >javascript statements;</script>The "Precautions" page JS code and Reference JS code can be embedded in the HTML anywhere, but the location of the different will affect the execution sequence of JS code. For example, the script tag executes the Js;② reference external JS file before the page is loaded in the body, using<scriptsrc= ". js"language= "JavaScript" ></script>Script tags referencing external JS files must appear in pairs and cannot have any code inside• Here is a description of the variables in JS:JS Variable declaration is different from C, C + +, Java and so on, to use int long char and so onall variable type declarations in ①js use the var keyword. The exact type of the variable depends on the type of the variable being assigned② The same variable can change the data type of a variable at multiple different assignmentsvar width = Ten; width is the shaping variablevar width = "One"; width is changed to string type③ variables can also be omitted by using Var④ the same variable name can be declared multiple times with Var, but no meaning, no error. After the second declaration, it will only be interpreted as an assignment.Although variables are defined only with Var, the variables in JS are of a data type:undefined undefined, the variable defined with VAR is not assigned a value. var A;Null represents a special variable type. The unique value is null, which is represented as an empty reference. Empty object, empty arraynumber value type. You can make a decimal, or it can be an integer. String string, enclosed in double quotation marks "" or single quotation marks, called Stringsboolean true, optional value True/falseobject complex Data typespeaking of which, of course, you should emphasize the naming conventions of variables:Variable names consist of alphanumeric underscores and $ characters, which cannot begin with numbers, and case-sensitive variable names are named to conform to the camel's rule: The variable starts with lowercase, followed by the first letter of each word;• There are several common numeric functions in JS that are important:①isnan is used to determine whether a variable or constant is Nan (non-numeric)
This is very very very much around using isNaN to determine when you try to convert by using the number () function, if it can be converted to numbers, it is not a non-numeric value, the result is false; for example: "11122" pure numeric string, false "" empty string, number converted to 0,false " 2la44 "Truetrue/false Boolean type, False②number () Convert other types to numeric types[String type to numeric] "string is a pure numeric string, will be converted to the corresponding number" 111 "->111" when the string is empty, will be converted to 0 "string containing other non-numeric characters, cannot convert" 111a "->nan[Boolean type to the numerical value]" true- > 1 false-> 0[null to value]null->0[undefined to value]undefined-> nan[object]* first call the value of method to determine if the function has a return value, then judging by the above situation③parseint () convert a string to a numeric value"Empty string result nan" "pure numeric string can go, if there is a decimal, erase after the decimal point, do not round" contains other character string, will intercept the first non-numeric value before the part, if the beginning of the letter, the result is Nan "123a33"-"123" A123 "-" Nan can only be converted to string types, boolean/null/undefined are Nan④parsefloat () converts a string to a numeric value, using the same method as parseintBut the decimal point is preserved when the decimal string is converted, and integers are preserved when converting an integer string. "123.5"->123.5 "->125"⑤typeof used to detect variable data typesUndefined--Returns a undefined string---returns stringtrue/false---Returns a Boolean value---Returns a Number object/null--Returns the object function--returns function

· The operators in JS

The operators in JS are the same as in C and Java, and there is no difference in the use of special annotations.

arithmetic Operations+ Plus, minus, * multiply,/divide,% withdraw, + + self-increment 、--self-reduction Assignment Operations= assigned value+=:a+=5 is equivalent to a=a-5, but the former is efficient to execute . -= *= /= %=Relational Operations= = equals, = = = Strictly equals,! = Not equal To, >, <, >=, <== = = Strictly equals, returns false for different types. The same type, then the next decision= =: same type, judging. Different types, different types, try to use the number () function on both sides of the equation to convert numbers, in the judgment. Exception: Null==undefined founded null===undefined not establishedconditional operator (multi-mesh operation)A>b?true:falseThere are two key symbols:? when? When the result of the preceding operation is true, execute: the front codewhen? When the operation at the front is false, execute: the code behind it;multiple layers can be nestedlogical Operators&&, | |,! the emphasis is on the precedence of the operator, when it is unclear, be sure to add parentheses! ()! ++ --* / %+ -> < <= >=== !=&&||assignment operator = + = =%=Make sure that you can go down to learn JS, and not be confused.

JS Basics-Prerequisites for entry

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.