JavaScript DOM Programming Art Learning Notes-chapter II JavaScript syntax

Source: Internet
Author: User
Tags logical operators

One, javascript example

1 <!DOCTYPE HTML>2 <HTMLLang= "en">3     <Head>4         <MetaCharSet= "Utf-8" />5         <title>Just a Test</title>6     </Head>7     <Body>8         <Scriptsrc= "Example.js"></Script>9     </Body>Ten </HTML>

Put the above code in the file and name the file test.html

Alert ("Hello world!")

Name the above code example.js and put it in the same folder as Test.html, and then open the test.html with your browser to see the results.

Two, syntax statements

JavaScript consists of a series of statements, one statement per line, and preferably a semicolon ending. If more than one statement is placed on the same line, you must separate them with semicolons.

Comments

Single-line Comment://

Multiline Comment:/* */

You can also use HTML-style annotations:<!--, but only one line is supported and the end does not need to be-->, and it is not recommended to use this notation to avoid confusion.

Variable

The thing that stores all kinds of variable data is called a variable . The operation of storing a value in a variable is called an assignment.

It is not necessary to declare a variable before using it, but it is a good practice to declare variables in advance.

Variables can be declared individually, such as

var mood;var age;

You can also declare multiple variables at once in a single statement:

var mood,age;

You can even declare the assignment to complete once,

var mood = "Happy", age = 23;

JavaScript variables are case-sensitive, and variable names allow only letters, numbers, dollar signs, and underscores, but the first character cannot be a number.

Data type

JavaScript is a weakly typed language that declares a variable without having to declare the type of the variable, and can change the data type of the variable at any stage.

Data types in javascript:

1, string

The string is made up of 0 or more characters, and the string must be in quotation marks, both single quotes and double quotes.

If the string contains single quotation marks, you can enclose it in double quotation marks or in single quotation marks with an escape character. That

var mood = "Don ' t ask";

var mood = ' don\ ' t ask ';

If the string contains double quotes, the same is true.

Using the same quote format for strings throughout your script is a good programming habit.

2, value

Values in JavaScript can be integers and floating-point numbers

3, Boolean value

There are only two possible values: True and False

Array

You can use an array to store multiple values, and the types of these values can be different. Declare an array using Arrary.

Cases:

var beatles = Array (4);

The above statement declares an array with four elements.

The behavior of adding elements to an array is called padding. When filling, you need to give the name and subscript of the array.

such as: beatles[0] = "John";

Note: The subscript for arrays in JavaScript starts with 0 .

If you do not know the length of the array, you can also declare this:

var beatles = Array ();

This is also possible in the following ways:

var beatles = ["John", "Paul"]; Create an array with square brackets

var lennon = ["John", 1940, false]; Include different types of values in the array

var mbeatles = []; Create an empty array with square brackets

Mbeatles[0] = Lennon; Array contains other arrays

Among them, mbeatles[0][0] is "John".

Associative arrays : using strings as arrays of array subscripts

var lennon = [];

lennon["name"] = "John";

lennon["Year"] = 1940;

lennon["Living"] = false;

It is not recommended.

Object

Similar to arrays, objects use a name to represent a set of values, and each value of an object is a property of the object. Such as:

1 var lennon = Object (); 2 lennon.name = "John"; 3 lennon.year = 1940; 4 false;

The above code creates an object using the Object keyword, or you can use curly braces to create an object

{Propertyname:value,....} The curly braces can also be empty. Cases:

var lennon = {Name: "John", year:1940, living:false};

Three, operation

Common operators: + 、-、 *,/

Also: self-added: + +, self-reduction:--

The add operator can be applied to the addition of string and numeric data, and string data.

There are + =,-= and so on.

Iv. conditional statements

if (condition) {statements;} Condition is a Boolean type

Comparison operators

Common comparison operators: >,<,>=,<=,==,===,!=,!==

where = = = is strictly equal to,! = = Strictly Not equal to

where = = has the following effect:

var a = false, B = "";

The result of a = = B is true.

logical operators

Logic and:&&

Logical OR: | |

Logical non-:!

Five, loop statement while loop:

while (condition) {statements;}

do {statements;} while (condition)

For loop

for (initial condition; test condition,; alter condition) {statements;}

Six, function

Defines the syntax for a function:

Function name (arguments) {

statements;

}

Scope of the variable

Global variables: can be referenced anywhere in the script. The scope is the entire script.

Local variable: exists only inside the function that declares it.

Global variables are masked when local variables declared in a function have the same name as global variables.

Seven, Object

An object is a very important data type, a self-contained collection of data that can be accessed in two forms by the data contained in the object-Properties and methods.

Property: A variable belonging to a specific object

Method: A function that only a specific object can call

An object is a data entity that is composed of a combination of properties and methods

In JavaScript, properties and methods use the "dot" syntax to access

Object.property

Object.Method ()

Objects need to be instantiated to be used, objects are instantiated with the new operator

such as: var John = new Person;

Built-in objects

Built-in objects: JavaScript pre-defined objects that we can use directly.

Host Object

In addition to the built-in objects, you can use some other objects that are already predefined in JavaScript scripts. These objects are not provided by the JavaScript language itself but by its operating environment. specifically to Web applications, this environment is the browser, and the predefined objects provided by the browser are called host objects .

JavaScript DOM Programming Art Learning Notes-chapter II JavaScript syntax

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.