Javascript Basics Basics

Source: Internet
Author: User

A. JavaScript introduction:
Before learning JavaScript, there are some things you need to know first:
Html
Xhtml
I don't think these need to go too deep, understand a probably on the line.
JavaScript is:
JavaScript is designed to add interactive behavior to HTML pages.
JavaScript is a scripting language.
JavaScript consists of several lines of executable computer code.
JavaScript is often embedded directly in HTML pages.
JavaScript is a kind of explanatory language.
All people can use JavaScript without purchasing a license.
These are some of the books on the definition of JavaScript, you know.
Two. A simple example of implementing javascript:
[HTML]
<body>
<script type= "Text/javascript" >
document.write ("This is JavaScript.");
</script>
</body>
When you embed JavaScript code in an HTML page, you need to add a JavaScript tag at the head and end to tell the browser that this is JavaScript code.
For example, the above example will be displayed on the page:
This is JavaScript.
If there is no red section code in the example, it will show:
document.write ("This is JavaScript.")
In the past, when older browsers did not support JavaScript, to prevent browsers from having JavaScript code as text output, we could write code as:

Copy Code code as follows:

<script type= "Text/javascript" >
<!--
document.write ("Hello world!");
-->
</script>

In this way, browsers that do not support JavaScript will automatically skip the code inside, and the support will execute the code inside.
In general, we try to separate the JavaScript code as much as possible, making the external file, one is to make the page code less confusing, and the second is to avoid duplication of JavaScript code when different pages are reused.
<script src= "Aaa.js" >....</script>
The. js file is generally unified in the subdirectory of the website to facilitate maintenance and increase the readability of the code.
three. JavaScript statements and annotations
JavaScript statements with no ";" can be, without the one with a behavior, with ";" can write multiple statements on the same line.
Copy Code code as follows:

<script type= "Text/javascript" >
document.write ("document.write ("World"); Output "World"
/* This multiline comment. */
</script>

As you can see from this simple example, you can embed HTML statements in JavaScript output statements, and you can try to write complex examples.
four. JavaScript basic Syntax
1. Declaring variables
var x;var x=1;var x= "Hello" .... Just one Var, very simple.
2. Operator
This is similar to other languages, there is nothing to say. Find a table on the Internet and see for yourself.
Operator description
+ Plus
-Minus
* Multiply
/except
% to find the remainder
+ + Additive
--Diminishing
The operator example is equivalent to
= X=y
+ + x+=y X=x+y
-= X-=y x=x-y
*= x*=y X=x*y
/= x/=y x=x/y
%= x%=y x=x%y
Interestingly, if two strings are added, the output is the result of concatenation of these two strings.
3.if,switch,for,while
These are all very simple, just take a look at the examples.
Copy Code code as follows:

<script type= "Text/javascript" >
var a=3
if (a==1)
{
document.write ("A's value is 1");
}
else if (a==2)
{
document.write ("A's value is 2");
}
Else
{
document.write ("A value is greater than 2");
}
</script>

Copy Code code as follows:

Switch (n)
{
Case 1:
Execute code block 1
Break
Case 2:
Execute code block 2
Break
Default
If n is not 1 or 2, then this code is executed
}

Copy Code code as follows:

for (i=0;i<=10;i++)
{
document.write ("The number is" + i)
document.write ("<br/>")
}

Copy Code code as follows:

var i=0
do{
document.write ("The number is" + i)
document.write ("<br>")
i++
}while (i <= 10)

Copy Code code as follows:

var i=0
while (I <= 10)
{
document.write ("The number is" + i)
document.write ("<br>")
i++
}

Copy Code code as follows:

For (variable in object)//traversal array commonly used
{
Executing code here
}

These are the basics, and next time I'll write the things that are commonly used in 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.