Home  >  Article  >  Web Front-end  >  Basic learning for getting started with Javascript_Basic knowledge

Basic learning for getting started with Javascript_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 18:32:481058browse

1. Introduction to JavaScript:
Before learning JavaScript, there are some things you need to understand first:
HTML
XHTML
I think these don’t need to be too in-depth, just a general understanding That’s it.
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 into HTML pages.
JavaScript is an interpreted language.
Everyone can use JavaScript without purchasing a license.
These are the definitions of JavaScript in some books, just know them.
2. Simple example of implementing JavaScript:
[html]





Embed JavaScript code into HTML pages When doing this, you need to add JavaScript tags at the beginning 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 part of the code in the example, it will be displayed:
document.write("This is JavaScript." )
In the past, when old browsers did not support JavaScript, in order to prevent the browser from outputting JavaScript code as text, we could write the code as:

Copy code The code is as follows:



In this case, browsers that do not support JavaScript will automatically skip the code inside, and browsers that support it will automatically skip the code inside. The code inside will be executed.
Under normal circumstances, we try to separate the JavaScript code as much as possible and make it into an external file. First, it makes the page code less confusing, and second, it avoids repeated writing when reusing JavaScript code on different pages.

.js files are generally placed in subdirectories of the website to facilitate maintenance and increase code readability.
3. JavaScript statements and comments
JavaScript statements can be written with or without ";". Those without ";" can be written in one line and one sentence. Those with ";" can be written in multiple statements on the same line. .
Copy code The code is as follows:



As you can see from this simple example, HTML statements can be embedded in JavaScript output statements. You can try to write some complex examples.
4. Basic JavaScript syntax
1. Declare variables
var x;var x=1;var x="Hello". . . . Just a var, very simple.
2. Operator
This is very similar to other languages, there is nothing much to say. Find a table online and see for yourself.
Operator Description
Add
- Subtract
* Multiply
/ Divide
% Find remainder
Accumulate
-- Decrement
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
What is interesting is that if two strings are added, the output is the result of concatenating the two strings.
 3.if, switch, for, while
These are very simple, just look at the examples.
Copy code The code is as follows:



Copy code The code is as follows:

switch(n)
{
case 1:
Execute code block 1
break
case 2:
Execute code block 2
break
default:
If n is neither 1 nor 2, then execute this code
}

Copy code The code is as follows:

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

Copy code The code is as follows:

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

Copy code The code is as follows:

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

Copy code The code is as follows:

for (variable in object)//Commonly used for traversing arrays
{
Execute code here
}

These are the most basic things. Next time I will cover the most common ones in JavaScript Write out the incident.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn