JavaScript basic Syntax (i)

Source: Internet
Author: User

In the previous period of time to learn HTML and CSS, but also combat some of the simpler structure of the project. Before the use of the knowledge of JS, the effect is always felt less. Although the overall layout and some basic effects can be done with html+css. But now began to go into JavaScript learning, after learning JS, only to be able to make a decent site. Learning JS is not as easy as learning HTML and CSS. If there is not enough patience and perseverance, it is estimated to be knocked down soon. Knowledge needs to accumulate a little, and eventually converge into the ocean of knowledge.
1. What is JavaScript
Javascipt is a scripting language that is interpreted and executed by a Web browser. The script is like the actor's script, which determines what the actor is going to do. Programs written in JavaScript can access the elements of a Web page and the browser that runs it, perform operations on those elements, and create new elements. JavaScript is an interpreted language, not a compiled language such as C + + or Java.
The instruction is passed to the browser in plain text and then interpreted in turn for execution.
2. What JavaScript can do
allows the page to move, including dynamic data, dynamic tags, dynamic styles, and more.
3. How JavaScript is applied to Web pages
(1), insert the JavaScript code into the <script> tag of the HTML Document
Example:

<script type= "Text/javascript" >
Alert (' Hello! ’);
</script>

</HEAD>
(2), the JavaScript code is stored in a separate file. Use. js as the file extension, and then use the SRC attribute of the <script> tag to point to the file.



<script type= "text/ JavaScript "src=" Test.js "></SCRIPT>
</HEAD>

(1), variable

A, the variable is defined by the var operator plus the variable name. As follows:
var age = 25;
Here, Var is used to define a variable named age, whose value is an integer 25.
Note: Variables are simple to understand as a container in life.

Variable name naming rules:

The first character must be a letter, an underscore (_), or a dollar sign ($);

The variable name cannot contain spaces or punctuation (except for underscores (_) and $);

b, variable names are case-sensitive;

Cannot use reserved words, keywords;
As follows:
var name= "John";
var 1num= "0"; wrong
var var=1; wrong

(2), data type

A, strings (string)

A string consists of 0 or more characters, including letters, numbers, punctuation, and spaces;

The string must be enclosed in quotation marks (single or double quotes);

var name= "sum";
var mood = ' happy ';

b, value (number)
var age = 25;
var price = 33.25;
Note: Note that the character of a string is enclosed in single or double quotation marks.

(3), JavaScript annotations

JavaScript annotations can be used to improve the readability of your code. JavaScript does not execute annotations. We can add comments to explain JavaScript, or to improve the readability of the code.

Single-line Comment

/* ... */ Multi-line comments

Note : Multiline comments cannot be nested with each other.

(4), Boolean type (Boolean)

Boolean data can only have two values of true and false;

var married = true;

var married = false;

Unlike strings, do not enclose Boolean values in quotation marks. A Boolean value of false and the string "false" are two different things.

(5), Objects (object)

In JavaScript, almost all things are objects. Object JavaScript is an object-oriented language, so you can use object-oriented thinking to make an object a data entity that is composed of a collection of properties and methods that are related to each other. An object is also a variable, but an object can contain multiple values (multiple variables)

In real life, a car is an object. The object has its properties, such as weight and color, and the method has a start stop and so on:

A, object definition

var person = {firstName:' John ', LastName:"Doe", Age:eyecolor:"Blue"};

Defining JavaScript objects can span multiple lines, and spaces and line breaks are not required:

var person = {
FirstName: "John",
LastName: "Doe",
Age : ,
Eyecolor: "Blue"
};

B, Object properties

It is generally assumed that the "JavaScript object is a container for key-value pairs".

Key-value pairs are usually written as Name:value (the keys and values are separated by colons).

Key-value pairs are commonly referred to as object Properties in JavaScript objects.

Ways to Access Object properties:

1), Person.lastname;

2), person["LastName"];


JavaScript basic Syntax (i)

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.