JavaScript Basics Learning Summary

Source: Internet
Author: User
Tags button type

JavaScript Summary
Basic features of 1 JavaScript
<1> Writing HTML output
document.write ("<2> Reacting to Events
<button type= "button" onclick= "alert (' welcome! ')" > Click here </button>
<3> Changing HTML content
X=document.getelementbyid ("demo")//Find elements
X.innerhtml= "Hello JavaScript"; Change Content
<4> changing HTML images
Element=document.getelementbyid (' myimage ') element.src= "/i/eg_bulboff.gif";
JavaScript can change most attributes of HTML, not just pictures
<5> Validate input
If IsNaN (x) {alert ("Not Numeric")};
<6> Change Style
X=document.getelementbyid ("demo")  //Find element x.style.color= "#ff0000";           Change Style
2 Use of JavaScript
<1> Location:
The script in the HTML must be between the <script> and </script> tags. Scripts can be placed in the <body> and <2> Conventions:
JavaScript is usually placed in the head or at the end of the HTML to be clear.
<3> Import Foreign JS file
You can also save the script to an external file. External files typically include code that is used by multiple Web pages.

The file name extension for external JavaScript files is. js.
<script src= "Myscript.js" ></script>
3 JavaScript output
<1> output by overwriting HTML properties
document.getElementById ("Demo"). Innerhtml= "My first JavaScript";
<2> output via document.write
document.write ("<p>my first javascript</p>");
Suppose you run document.write after the document has finished loading, and the entire HTML page is overwritten.


4 JavaScript statements
<1> statement function is to tell the browser what to do
<2> run sequence is run step by piece in writing order
<3> semicolon flag statement end, but optional
The <4> code block is enclosed in curly braces "{}". Representing statements to run together
<5> is sensitive to uppercase and lowercase
<6> connect rows by ' \ '
document.write ("Hello \
World! ");


5 JavaScript Gaze
JavaScript's gaze style is the same as C + +


6 JavaScript variables
<1> Variable Declaration
Variables can be declared with Var. or simply by assigning a value to a variable and eliminating var.
var x = 12;
y= 18;
<2> multiple variable declarations
var name= "Gates", age=56, job= "CEO";


7 JavaScript data types
The data types in JavaScript contain numbers, Booleans, strings, and arrays. Object. null,undefined
<1> var x; Undefined
var y1 = ' Sasa ' String
var y2 = "SDSD" String
var z = False bool

<2> objects
Objects are separated by curly braces.

Inside the parentheses, the properties of the object are defined in the form of name and value pairs (name:value). Attributes are separated by commas:
var person={firstname: "Bill", LastName: "Gates", id:5566};


Addressing the object:
Name=person.lastname;
name=person["LastName"];


<3>undefined
var xx; //undefined declaration but not initialized


<4>null
var car=null; //used to empty variable values


<5> New
When you declare a new variable. The keyword "new" can be used to declare its type:
var carname=new String;
var x= new number;


<6> Array
var cars = new Array ("Audi", "BMW", "Volvo"); Array
var cars = ["DSD", "Dqwd", "DWDEWF"]; //array
8 JavaScript objects
All things in JavaScript are objects: strings, numbers, arrays, dates, and so on. In JavaScript. Objects are data that have properties and methods.
A new method for defining objects

Person=new Object ();p erson.firstname= "Bill";p erson.lastname= "Gates";p erson.age=56;person.eyecolor= "Blue";

9 Functions in JavaScript
function functionname () {//Here is the code to run}

The use of functions is similar to the use of java,c++.
Although there is no return value type outside the function. But the function can have a return statement.


The operators in the ten JavaScript
JavaScript has +-*/% + +--= = *=%=/=. How to operate with C + +
Note: Numbers and strings are added to convert numbers into strings.


Conditional statements in one-to-one javascript
If-else,switch statements
These two statements are basically similar to C + +


The Loop statement in JavaScript

For-loop code block for a certain number of times
For/in-Looping through the properties of an object
While-loops the specified block of code when the specified condition is true
Do/while-the same code block that is looped when the specified condition is true


Note the for-in loop, which is used for object property traversal
var person={fname: "John", lname: "Doe", age:25};
for (x in person)  {  txt=txt + person[x];  }

Characteristics of the break statement
With a label reference, the break statement can be used to jump out of any JAVASCRIPT code block:
<script>cars=["BMW", "Volvo", "Saab", "Ford"];list:{document.write (Cars[0] + "<br>"); document.write ( CARS[1] + "<br>"); document.write (cars[2] + "<br>"); Break List;document.write (Cars[3] + "<br>"); }</script>

Operation Result:
BMW
Volvo
Saab


The anomaly mechanism in JavaScript
The Try statement tested the code block for errors.
The catch statement handles the error.
The throw statement creates its own definition error.
Demo Sample code:
<script>function myFunction () {try  {  var X=document.getelementbyid ("Demo"). Value;  if (x== "")    throw "empty";  if (IsNaN (x)) throw "not a number";  if (x>10)     throw "too high";  if (x<5)      throw "too low";  } catch (Err)  {  var Y=document.getelementbyid ("mess");  Y.innerhtml= "Error:" + Err + ".";  }} </script>


JavaScript Basics Learning Summary

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.