JavaScript self-organizing the basics-01

Source: Internet
Author: User

1.JavaScript Introduction:

JavaScript is the most popular scripting language on the Internet, and all modern HTML uses JavaScript. Since it is a scripting language, there are three types of features:

(1) weak type;

(2) interpreted language (no compilation required);

(3) Row by line execution, one line of code error, the subsequent code block will not continue to execute;

(4) <script> tags can be directly embedded in the HTML file, the location is arbitrary, usually placed under the modified content or head tag, but written as a separate JS file is conducive to the separation of structure and behavior

2.JavaScript content (drawings):

Where ECMAScript is the core of JavaScript;

Dom is a Document Object model (using JS to manipulate web pages);

BOM is the Browser object model (using JS to manipulate the browser)

Output of 3.JavaScript information:

(1) Alert () method: In the form of a cue box in the page output

(2) Console.log () method: Output Information in console, example:

Console.log ("Hello,javascript")   

(3) document.write (): Write the content directly in the HTML page, for example:

4.JavaScript variables:

Unlike Java, a variable in ECMAScript has no specific type, the variable is defined with the var operator only, it can be initialized to any value, the initialization format of the variable: var variable name = variable value;

var a = "Hello";   var b = 123;  

If you want to define multiple variables, you can use multiple variables in one line, separated by commas, for example:

var a = "Hello",      = 123,      

Variable rules for variable names:

(1) consisting of letters, numbers, underscores, and $ symbols

(2) can not start with a number, do not suggest the following dash start;

(3) strictly distinguish the case;

(4) Can not be keywords and reserved words

5.JavaScript Data type:

JavaScript can be divided into raw data types, reference data types, two kinds:

(1) Raw data type: Number,string,boolean,undefined,null

Number: Numeric, numeric, contains positive, negative, integer, decimal, 0, NaN, Infinity (positive infinity),-infinity (negative infinity);

Note:NaN: The abbreviation for not a number, indicating that the value is not a numeric (also belongs to #)

string: 0 or more characters wrapped with double quotation marks "" or single quotation marks, if there is nothing in the quotation marks, then this string is called an empty string.

Boolean: Boolean: Contains true: Indicates True (SET) and false: False (not set) two values

undefined: Indicates that the variable is undefined, or the variable is defined, but is not assigned a value

null: Indicates that a variable does not point to any piece of storage space, that is, the variable exists, but the inside is empty, similar to undefined

(Tip: In the chrome console output, you will find the number type is dark blue, string is black, the boolean is light blue, undefined and null are dimmed)

(2) Reference data type:

Object, Array (array), date (date), REGEXP (Regular): Wait a minute

(3) How to view the data type of a variable (typeof operator):

Numeric data: The return value is number

Console.log (typeof 123)   //

String data: The return value is string

Console.log (typeof "Hello")  // output string    

Boolean data: The return value is Boolean

Console.log (typeoftrue/false)    

Undefined: The return value is Undefined

Console.log (typeof undefined)   // output undefined

Null: The return value is object (legacy problem, indicating null is also an object)

Console.log (typeofnull)     //    

NaN: The return value is number

Console.log (typeof NaN)    //

Conversion of 6.JAVASCRIPT data types:

(1) When using the addition (+) operator, any data that is added to the string type data is string type data;

Console.log ("Hello" + 123)    // output "Hello 123"  

Note (simple understanding): in JavaScript Hollow string "" is converted to false, non-empty string is converted to true (except "0", "1");

False is converted to 0 or "0", and true to 1 or "1";

When making logical judgments, null,undefined, "" (empty string), 0,nan all default to false;

= = In comparison can be converted data type, = = = is strictly compared, as long as the type mismatch will return false;

In fact, == the comparison is actually converted to a string to compare, but before the Boolean is converted to a string, it is first converted toNumber

Console.log ("123" = =true)//Output FalseConsole.log ("1" = =true)//Output TrueConsole.log ("" = =true)//Output FalseConsole.log (1 = =true)//Output TrueConsole.log ("" ==false)//Output TrueConsole.log (' 123 ' = =false)//Output FasleConsole.log (' 0 ' = =false)//Output TrueConsole.log (0 = =false)//Output TrueConsole.log (' 1 ' = = 1)//Output TrueConsole.log (' 0 ' = = 0)//Output TrueConsole.log (-true)//Output-1

(2) Parseint: Converts a string to an integer (only the numeric value in the string is recognized):

Note: The front and back spaces in the string are ignored (they will not be recognized when there are several values after the number of spaces);

Can correctly identify the positive and negative sign, that is, retain the sign;

A non-numeric character will stop converting when it is converted;

If the first character of a string is non-numeric, the result of the conversion is Nan;

Console.log (parseint ("123"))    // output 123  Console.log (parseint ("1 2"))    //  Only outputs 1  console.log (parseint ( -123))     // output -123  console.log (parseint ("Hello") )    // output nan  console.log (parseint (true))       // output Nan   Console.log (parseint ("123hello"))    // output 123, the back non-numeric type  does not recognize console.log ( parseint ("1"))     // output 1, ignore spaces  

(3) Parsefloat: Convert String to decimal (identify decimal point, note above)

Console.log (parsefloat ("123.55"))    // output 123.55  console.log (parsefloat (". 1hello"))    // Output 0.1  

(4) Number: Convert other types of data into numerical form, note that the converted data must be a pure numeric composition, otherwise cannot be converted, other considerations as above

Console.log (Number (true))   //1  console.log (number (false))    / /0  console.log (number (null))    //0  Console.log ( Number ("123hello"))    //NaN  console.log (number ("12.22"))    //  12.22  console.log (number (undefined))    //

(5) The Information box in the page:

Alert (), pop up a hint box, only OK;

Window.alert ("The weather is fine today")  

Confirm (), pops up a confirmation box, has OK and cancel;

Window.confirm ("Good mood Today")  

Prompt (), pop up an input box, you can enter the content;



The basics of JavaScript are written here for the time being, and the follow-up will fill up ...

JavaScript self-organizing the basics-01

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.