Basic JavaScript Concepts

Source: Internet
Author: User

First, use JavaScript in HTML

1. Use the <script></script> tag directly.

2. External Introduction

<script type= "javascript" src= ". /app/js/test/test1.js "></script>

Generally will be placed at the bottom of the browser, so that the page is loaded first, to avoid the JS file too large, the page load delay, there has been a blank situation.

Document type

DOCTYPE is a shorthand for document type, which is used in pages to specify the version of XHTML (or HTML) used by the page.

The different document modes mainly affect the rendering of CSS content, especially the browser's parsing of the box model, but in some cases it will also affect the execution of JavaScript interpretation.

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01//en" "HTTP://WWW.W3.ORG/TR/HTML4/STRICT.DTD" >

< Span class= "Apple" >

If the document type declaration is not found at the beginning of the document, all browsers will turn on promiscuous mode by default!

In general: promiscuous mode makes IE like IE5, and Standard mode uses the IE7 rendering engine.

Second, JS basic concept

1. Syntax

Good programming Habits: Note The code specification, the semicolon at the end of the statement is not required, but a semicolon also improves the performance of the code in some cases, because the parser does not have to spend time speculating where the semicolon should be inserted.

Variables: ECMAScript variables are loosely typed, and each variable is just one for saving a worthy placeholder.

2.6 Types of data

Number, String, Boolean, Undefined, Null. (Basic data type)

Object (Reference data type) is actually like an array, a function is also a reference type

You can use the TypeOf operator to determine which basic data type:

var message = "Something" alert (typeof message);   " String

Mention the basic Package type: JS provides three basic packaging types: number, String, Boolean, which allows us to manipulate these basic data.

We know that the basic type is not an object and there is no method. In fact, each time a basic type is created, the background automatically creates an object of the corresponding basic wrapper type, which is stored in memory so that we can invoke some methods to process the basic data.

Number

Integer: 3 is usually used in decimal.

Floating-point: 3.14 The highest precision of a floating-point value is 17-bit if (0.1 + 0.2 = = 0.3) {console.log ("correct")} is wrong, do not do such a test, because the computer's underlying binary algorithm is not the same.

Numeric conversions: (Basic not how to use, to understand just fine)

var num1 = parseint (22.5) //22 (not rounded) var num2 = parseint ("123blue") //123 converted to integral type 
   
    var num1 = parsefloat (22.5) 
    //
    22.5 var num2 = parsefloat ("098.5") //98.5 converted to floating-point type
   

How to: (this is the point)

var num = 3.145;  Num.tofixed (2); // 3.15

Monolithic built-in objects some of the ways math handles numbers:

Math.max (3.14,4.14) //4.14 math.min (3.14,4.14) //3.14math.floor(3.54) //
    3 math.ceil (3.14) //4  math.round (3.14) //3 math.round (3.54) 4 Rounding rounding Math.random ()  //0.2345 a random number greater than or equal to 0 less than 1 math.abs (-3) //  3

String

Strings are immutable, that is, when strings are created, their values cannot be changed. To change the string saved by a variable, first destroy the original string, and then populate the variable with another string containing the new worth.

Operation Method:

var age = 20;  Age.tostring (); // "a" var name = "Tom" name.length //3var string = "ABCDE";  var result =string.slice (3,4); // D var result1 =string.split (); // ["ABCDE"]  var result2 =string.split ("); //[" A "," B "," C "," D "," E "]
Split splits are broken down by what symbols in the string, and if separated by an empty string (""), each character in the string is "," to be split.

 var result3 =string.replace ("C", "F"); //"Abfde" (can be used for replacement)  

 var result4 =string.replace ("C", ""); //"Abde" (can be used to delete)

 var result5 =string.charat ("3"); //d 

 var result6 =string.indexof ("C"); //2

 var result7 =string.trim ();

 var result8 =string.touppercase (); var result9 =string.tolowercase ();

Boolean

A Boolean that has only two values of true and false to automatically execute the response as if judged

Undefined

The undefined type has only one value, which is undefined, and the value of the variable is undefined when the var operator is used to declare the variable but is not initialized, the difference is not defined and not initialized

Null

A null type has only one value, which is null, which represents an empty object pointer, the variable that defines the value is the variable that prepares the object, and is initialized to NULL before the variable is saved

Because a variable defined as a null value is prepared to hold the object, the variable is retrieved with the typeof operator and the return value is object.

If the defined variable is intended to be used to hold the object in the future, then the best variable is initialized to null instead of the other value.

Undefined derives from a null value, so undefined==null; True

Basic JavaScript Concepts

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.