JS Basic Finishing

Source: Internet
Author: User
Tags script tag

three ways to use JS

1, directly in the HTML tag, using the event properties, call JS code

<button onclick= "alert (' Bullet frame ')" > Pinball! </button>


2, in any location of the page, using the script tag, insert JS code

<script type= "Text/javascript" >//js code </script>


3. Introduction of external JS file

<script src= "New_file.js" type= "Text/javascript" ></script>


The ①JS code can be used anywhere in the code, but the placement is different and will affect the order of JS execution
② introduction of External JS in the script tag, no longer contain any JS code

the variables in JS 1, the Declaration of variables:

VAR num=1;//variables declared with var belong to local variables and are valid only at the current scope
num= "Naysayers Penguin"//variable without VAR declaration, default is global variable, available in this JS file
var a=1,b,c=3;//uses a single line of code to declare multiple statements where B is undefined

  "Considerations for variable declarations in JS"
①JS the variable keyword is only one var, the type of the variable depends on the value assigned
Undefined type if no value is assigned after declaration

The same variable in ②js, can be modified in multiple assignments, data type

The ③ variable can be declared by using Var, or directly by assigning a value.
Difference: Use Var to declare a scope as a local variable

④ in JS, a variable can use the Var declaration multiple times, declaring the equivalent of an assignment.

⑤js variable names are case-sensitive

2, JS data type:

Undefined: using var declaration, but no assignment
Null: Represents an empty reference
Boolean: TRUE or False
Number: Numeric type, including integral and floating-point types
String: Strings
Object: Objects

  3. Common numerical functions 

①isnan: Used to detect if a variable is not a value (not a namber)
isNaN when detected, the number function is called first, attempting to convert the variable to a numeric type, or Nan if the final result can be converted to a value

②namber functions: Converting various data types to numeric types
Undefined cannot be converted, return Nan
Null is converted to 0
Boolean true 1 False 0
If the string is a pure numeric string, you can convert "123"-->123
If there is a digital letter that cannot be converted
If an empty string is converted to 0

③parseint (): Converting a string to a numeric type
String if an empty string cannot go back to Nan
If it is a pure numeric type can be converted, and the decimal point directly to "123.8"-->123
If the string contains a non-numeric character, the integer preceding the non-numeric character is converted "1a1"-->1

④parefloat: The conversion mechanism is the same as Pareint ()
The difference is: turn decimals, there are decimal points 123.4-->123.4

⑤typeof: Detecting the data type of a variable
String returns the number True returns a Boolean undefined return undefined object/null returns the function

input and output statements commonly used in JS

1. Alert (): Pop-up window output

2, prompt: Pop-up window input receive two parts parameters: ① input prompt content, ② input box default content, both parts can be omitted.

The input content is a string by default.

3, document.write: In the browser content printing

4, Console.log: Browser console printing

the Declaration and invocation of functions in JS

    1. Format of function declaration     


Function name (parameter 1, parameter 2) {//Function body code return value;}

Function call:
① direct call, function name (multiple parameters);
② is called through an event,

<button onclick= "func (Prompt (), ' 444 ')" > Point you </button>

   2, function declaration and call considerations:

There is no return value in the ① function, only depending on whether the function has return. No need to declare
No return value, received as undefined
②js, there is no association between the formal parameter list of the function and the argument list
The actual number of function arguments, depending on the argument list
3, JS, the function is the only scope of the variable: (other places declare variables are global variables)
A function's formal parameter is a local variable that belongs to a function

4, the Declaration of functions and the invocation of the statement has no successive points, can be called after the declaration

"Order of Execution of code"
JS code execution divided into two stages, check the compilation phase, code execution phase
Check the compile phase: Check for syntax errors, declaration of a variable's declaration function
Code execution phase, assignment of variables, execution of function calls

   Func ();   var func=function () {alert (99); }

Execute the var func first
Then execute func ();
Func=function () {
alert (99);
So the call to the function is placed after the declaration
function func () {
Alert (1);
Func ();

Declaration and use of anonymous functions

   1. anonymous function expression:

var func=function () {}

2. Assign the anonymous function directly to an event

Window.onload=function () {}//document-ready function, make sure that the code in the function executes Window.onload=function () {document.getElementById ("div") after the HTML has finished loading . Onclick=function () {}}


3. Self-executing function
①!function () {} (): Start with! Represents a self-executing statement
② (function () {} ()): Use () to wrap an anonymous function declaration with a call
③ (function () {}) (): Declares an anonymous function with a () package

JS Basic Finishing

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.