Getting Started with JavaScript

Source: Internet
Author: User

Read Catalogue (Original-moe little Q)

    • One, you know, why is JavaScript so worthwhile for us to learn?
    • Second, easy to learn sex
    • Three, where to start learning?
    • Iv. How to insert JS
    • V. Quoting JS external file
    • Six, JS in the position of the page
    • Vii. Understanding Statements and symbols
    • Viii. Annotations are important
    • Nine, what is a variable
    • Ten, Judgment statement
    • Xi. what is a function
Back to Top One, you know, why is JavaScript so worthwhile for us to learn?

1. JavaScript is supported by all major browsers.

2. Currently, most Web pages worldwide use JavaScript.

3. It allows Web pages to present a variety of dynamic effects.

4. As a web developer, JavaScript is an essential tool if you want to provide a beautiful web page and a user-friendly Internet experience.

Back to the top of the second, easy to learn sex

1. There is no outside learning environment, as long as there is a text editor, you can write JavaScript programs.

2. We can do some basic operations with simple commands.

Back to Top three, where to start learning?

The starting point for learning JavaScript is working with Web pages, so let's start with the basic syntax and how to use the DOM for simple operations.

code Example 1:

1 <! DOCTYPE html> 2 
Back to top four, how to insert JS

Let's take a look at how to write JS code? You can use the <script> tag to insert JavaScript code in an HTML page in one step. Note that the <script> tag should appear in pairs and write the JavaScript code <script></script> between them.

<script type="text/javascript">Indicates that the text type is between <script></script>, JavaScript is meant to tell the browser that the text is in the JavaScript language.

Back to top v. Reference JS external file

With previous knowledge learning, we know that using the <script> tag to add JavaScript code to an HTML file:

Can javascript code be written only in HTML files? Of course not, we can separate the HTML file and JS code, and create a separate JavaScript file (JS file), its file suffix is usually. js, and then the JS code directly in the JS file.

Note: In the JS file, you do not need to <script> tags, directly write JavaScript code on it.

JS file can not be run directly, embedded in the HTML file to execute, we need to add the following code in HTML, the JS file can be embedded in the HTML file.

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

Back to top six, JS in the position of the page

We can place JavaScript code anywhere in the HTML file, but we usually place it in the head or body of the page.

    • Put in the

The most common way is to place the <script> element in the head section of the page, and the browser parses the head section to execute the code before parsing the rest of the page.

    • Put in the <body> section

The JavaScript code executes when the page reads to the statement.

Note: JavaScript can be placed anywhere in the HTML page as a scripting language, but the browser interprets the HTML in order, so the preceding script is executed first. For example, the page display initialization of JS must be placed in the head, because the initialization is required to advance (such as the page body set CSS, etc.), and if the function is executed through the event call the position is not required.

Example code 2:

1 <! DOCTYPE html> 2 

Operation Result:

I love You

Back to the top seven, recognize statements and symbols

JavaScript statements are commands that are sent to the browser. The purpose of these commands is to tell the browser what to do.

Each line of JavaScript code format: 语句;

Let's take a look at the following code

<script type= "Text/javascript" >   alert ("hello!"); </script>

In the example alert("hello!"); is a JavaScript statement.

The end of a line is considered the end of the statement, usually with a semicolon at the end ";" to represent the end of the statement.

Look at this code, there are three statements, each sentence after the end of ";", in order to execute the statement.

<script type= "Text/javascript" >   document.write ("I");   document.write ("Love");   document.write ("JavaScript");</script>

Attention:

1. ";" Semicolon to enter in the English state, the same, JS code and symbols are entered in the English state.

2. Although the semicolon ";" can also not write, but we have to develop a good habit of programming, remember to write a semicolon at the end of the statement.

Back to top eight, notes are important

The role of annotations is to improve the readability of your code, to help you and others to read and understand the JavaScript code you write, and the content of the comments will not be displayed on the page. Comments can be divided into single-line comments and multiline comments.

For readability, the comment is generally placed at or around the end of the statement that needs to be interpreted.

A single-line comment, with the symbol "//" before the content of the comment.

<script type= "Text/javascript" >  document.write ("single-line annotation using '//'");  I am the comment that the statement function outputs content in a Web page </script>

Multiline comments start with "/*" and End with "*/".

<script type= "Text/javascript" >   document.write ("Multi-line Comment use/* Comment content */");   /*    Multiline comment    develop good habit of writing notes   */</script>
Back to the top nine, what is a variable

What is a variable? Literally, a variable is a variable amount; From a programmatic standpoint, a variable is a memory used to store a certain number of values. We can think of variables as a box, in order to distinguish the box, you can use Box1,box2 and other names to represent different boxes, BOX1 is the name of the box (that is, the name of the variable).

Define variables using the keyword VAR, with the following syntax:

var variable name

Variable names can be named arbitrarily, but follow the naming conventions:

1. The variable must start with a letter, an underscore (_), or a dollar ($) symbol.

2. You can then use any number of English letters, numbers, underscores (_), or dollar ($) characters.

3. You cannot use JavaScript keywords with javascript reserved words.

Variables must be declared and then assigned, as follows:

var mychar;mychar= "JavaScript"; var mynum = 6;

Variables can be assigned repeatedly, as follows:

var mychar;mychar= "javascript"; mychar= "Hello";

Attention:

1. In JS, case-sensitive, such as variable MyChar and MyChar is not the same, the expression is two variables.

2. Variables Although can also not be declared, directly used, but not standardized, need to first declare, after use.

Back to top ten, judgment statement

The If...else statement executes the code when the specified condition is true and executes the else code when the condition is not established.

Grammar :

if Conditions {Code executed when condition is set} Else {Code executed when the condition is not valid}

Let's say we age to determine whether an adult, such as the age of 18 years old, is an adult, or not an adult. The code represents the following :

<script type= "Text/javascript" >   var myage =;   if (myage>=18)  //myage>=18 is the judging condition   {document.write ("You are an adult. ");}   else  //otherwise age less than   {document.write ("Under 18, you are not an adult.") ");} </script>
1 <! DOCTYPE html> 2 
Back to top 11, what is a function

A function is a set of statements that complete a particular function. Without a function, it may take five lines, 10 rows, or even more code to complete the task. In this case, we can put the code block that completes the specific function into a function, call this function directly, save the trouble of repeatedly inputting a lot of code.

How do you define a function? The basic syntax is as follows:

function name () {function code;}

Description

1. function defines the keyword for the functions.

2. "Function name" you take the name of the function.

3. "Function code" is replaced with code that completes a specific function.

Let's write a simple function that implements the addition of two numbers and give the function a meaningful name: "ADD2", the code is as follows:

function Add2 () {   var sum = 3 + 2;   alert (sum);}

Function call:

Once the function is well defined, it cannot be executed automatically, so it needs to be called, just write the function directly where it is needed, and the code is as follows:

Getting Started with JavaScript

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.