JavaScript Basics Tutorial--Getting started to read the basics

Source: Internet
Author: User
Tags anonymous arrays

JavaScript He is a descriptive language, in fact, he is not difficult to learn, as long as the intention to learn, will certainly learn, I believe that we read this article, must have learned HTML bar, using JavaScript is to be able to and Web pages have better interaction, the following into the theme.

A. JavaScript

1. What is JavaScript

JavaScript is a descriptive language and a scripting language that is based on objects (object) and event-driven (event driven) and has security.

The characteristics of 2.JavaScript

JavaScript is primarily used to add interactive behavior to HTML pages.

JavaScript is a scripting language, and syntax is similar to java.

JavaScript is typically used to write client script.

JavaScript is a kind of explanatory language.

The composition of the 3.JavaScript

Ecmscript standard (Specify all attributes, methods, and objects)

BOM Explorer Object Model (Browser object mode): Interacting with HTML

DOM Document Object model: Accessing and manipulating HTML documents

4.JAVASCRIPT Basic Structure

<script language= "javascript" type= "Text/javascript" >

</script>

language= "JavaScript" Used to indicate that the language used is JavaScript

5.JavaScript Execution principle

1. The browser client sends the request to the server side. (The address that the user enters in the browser's address bar)

2. Data processing: The server side handles a page that contains JavaScript.

3. Send a response: The server side will contain JavaScript HTML file processing page sent to the browser client, and then by the browser client from the top straight down the HTML tags and JavaScript tags, the page effect presented to the user.

Two How to introduce JavaScript into a Web page

1. Use <script> label.

2. Use an external JavaScript file.

You want to run JavaScript on multiple pages to achieve the same effect, typically using a. js file with an external file.

How to refer to. js as an extension file:

<script src= ". /1.js "></script>

Note: External files cannot contain <script></script>

3. Directly in the HTML tag

<input name= ' btn ' type= button "value=" pop-up message box "onclick=" Javascript:alert ("Welcome"); " />

Three. JavaScript core syntax

1. Declaration and assignment of variables

Variables are declared only with Var, and the naming specification for variables is similar to java. Var Num=1;

Variables can be used directly without declarations in JavaScript, but this usage is not recommended.

2. Data type

Undefined (undefined type)

Null (empty type)

Number (numeric type)

String (String type)

Boolean (Boolean type)

The difference between 3.undefined and null

Null means "No object", where there should be no value. Typical uses are:

(1) as a parameter of the function, which indicates that the parameter of the function is not an object.

(2) As the end point of the object prototype chain.

Object.getprototypeof (Object.prototype)/null

Undefined means "missing value," where there should be a value, but not yet defined. Typical uses are:

(1) When a variable is declared, it is equal to undefined when it is not assigned a value.

(2) when calling a function, the supplied argument is not supplied, which equals undefined.

(3) The object has no assigned property, and the value of the property is undefined.

(4) When the function does not return a value, the default returns undefined.

4. Only 6 cases were found to be false.

null,false,undefined,0, "", NaN

5. Some common methods of string

ToString (); return string

Tolowercasee (); Replace the string with lowercase.

toUpperCase (); converting strings to uppercase

CharAt (index); Returns a string at the specified position

IndexOf (Str,index); Finds the first occurrence of a specified string in a string

Substring (Index,index); Returns the string that is between the specified index index1 and index2 (including index1 excluding INDEX2)

Split (str); split a string into an array of characters

6. Three ways to create an array and assign a value to a group

Var num= (' 1 ', ' 2 ');

02.var num=new Array (2);

Num[0]=1;

num[1]=2;

03.var num=[' 1 ', ' 2 '];

Common methods and properties for arrays
Length: Sets or returns the number of elements in an array

Join (): Put all the speed of the array into a string, split by the delimiter.

Sort (): Sorting the array

Push (): Adds one or more elements to the end of the array and puts the new length back.

<script type= "Text/javascript" >

01. Mode One: Create an array and assign values to the array

var fruit = new Array (' Apple ', ' orange ', ' peach ', ' bananer ');

02. Mode Two: First create the array, and then through the subscript to the array to assign values

var fruits = new Array (4);

Fruits[0] = ' Apple ';

Fruits[1] = ' orange ';

FRUITS[2] = ' peach ';

FRUITS[3] = ' bananer ';

03 mode three: And way a similar just changed the symbol

var fruitss = new array[' Apple ', ' orange ', ' peach ', ' bananer ';

04. Access the data below the array labeled 3

document.write (Fruits[0]);

05 The element is placed in a string by the join method of the array and split with the specified delimiter

var result = Fruits.join (', ');

06 Sorting Arrays by Sort method

Fruits.sort ();

06. Add one or more elements to the end, and finally return the new length of the array

var length= fruits.push (' Wert ', ' foot ');

document.write (' \ n ' + length);

07. Traverse Array

For (var item in fruits)

{

Alert (Fruits[item]);

}

</script>

7. Operator

8 Program debugging

Scenario One: Debugging in VS

Step: 01. Set the page to be debugged as the starting page

02. Set Breakpoints

03. Press F5 to start debugging

Scenario Two: Chrome browser debugging

Step: 01. Click F12 to pull up the tool

02. Set Breakpoints

03. Refresh Page

Program Three: IE browser

Step: 01. F12, developer tools

02. Cut To Script tab

03. Set Breakpoints

04. Start Debugging

05. Refresh

Four. Functions in JavaScript

1. Common system functions

parseint ("string");

The parseint () function first looks at the character at position 0, determines whether he is a valid number, returns Nan if it is not, does not perform another operation, but if the character is a valid argument, the function will look at the character at position 1 and do the same test. This process continues until the character is found to be a valid character, at which point the character converts the previous string to a number.

eg

var num1=parseint ("78.9")//return value is 78

var num2=parseint ("afa78.9")//return value is Nan

02.parseFloat ("string");

His usage is similar to parseint, except that the first point that appears in the string is considered a valid character.

eg

var num1=parseint ("78.9")//return value is 78

var num2=parseint ("afa78.9")//return value is Nan

2. Custom function

In JavaScript, a custom function is made up of functions, function names, a set of parameters and JavaScript statements to be executed in parentheses.

Here's a look at the syntax:

Function name (parameter 1, parameter 2,..)

{

JavaScript statements

[Return value]

}

A function is a keyword that defines functions that must be.

Parameter 1, and parameter 2 is the parameter of the function, because JAVASCRITP is a weakly typed type, all of which are not necessary to provide a type when given a parameter

{} The start and end of the function defined.

The return statement is used to specify the value returned by the function.

2. Call function

To perform a function, you must first call this function, and you have to make the name of the functions and the arguments that follow.

eg

<script type= "Text/javascript" > Function Show ()

{



}

</script>

3. anonymous function

An anonymous function is a function without a name, also called a closure function (closures), which allows temporary creation of a function without a specified name. Most often used as a value for a callback function (callback) parameter, many novice friends do not know about anonymous functions. Let's analyze it here.

function function name (argument list) {functional body;}

If you are creating an anonymous function, it should be:

function () {functional body;}

Because it is an anonymous function, there is generally no parameter passed to him.

Why do you want to create an anonymous function? Under what circumstances the anonymous function is used. There are two common scenarios for anonymous functions, one is callback function and the other is direct execution function.

eg

<script language= "JavaScript" >
var a = "a";
(function () {
  var a= "B";
  alert (a);
}) ();
alert (a);
</script>

In the above code, two alert boxes are output sequentially. The first alert box has a content of B and the second is a. When an anonymous method is invoked in the script label, all the alert (a) that pops up first and then touches the anonymous method pops up a.

The above JavaScript basic tutorial--Getting started is to see a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.