JS Basic syntax

Source: Internet
Author: User
Tags string to number

1. What is the purpose of learning JavaScript?

A. Enhance the dynamic effect of Web pages.

B. Change the elements in the Web page (to manipulate elements directly on the page).

C. Enhance data interaction with the backend. Data validation for the page.

What is the role of 2.JS in web development?

JS is written on the page.

JS's operating environment:

1) is an interpreted language that is interpreted and executed by the browser . (Java is executed through Javac)

2) Run the JS code directly through the browser.

3.JS code writing?

1) The introduction of JS code in the Web page?

By adding <script> </script> tags anywhere on the page. is to tell the browser that the content in this tag is a scripting language.

<script type= "Text/javascript" >

document.write ("HelloWorld");

</script>

2) directly through the page element introduction

<a href= "Javascript:alert (' clicked Me ');" > I'll try </a>.

3) Separate JS file (when developing, it is recommended to use JS to form a separate file)

<script src= "Js/first.js" ></script>

4.JS data type?

JS is a weak type of language.

A. Numeric type:

Represents a number.

B.boolean type

Represents true or False

C. String type:

Represents a string, double quotation marks, or single quotation marks are all possible.

D. Date type. Array type ...

Syntax for variable definitions:

var a=12;//a is the variable name

Note the issue:

1) When defining a variable, be sure to assign a value, if not assigned, is the special type: undefined type.

5. Conversions between data types.

1) Convert other data types to number type:

The problem of mutual conversion between types

var a= "123ABC";

You need to convert the string to number type

var b= parseint (a);

document.write (b);

If there is an error in the conversion, a Nan (not a number) appears. For example, the following conversions will get the Nan type:

The problem of mutual conversion between types

var a= "EF123ABC";

You need to convert the string to number type

var b= parseint (a);

document.write (b);

2) Other types are converted to string type.

The problem of mutual conversion between types

var a=123;

You need to convert the string to a string type

var b= a.tostring ();

Another way to convert to a string:

The problem of mutual conversion between types

var a=123;

You need to convert the string to a string type

var b= a + "";

6. When using JS, the interaction with the browser.

1) The interaction of the popup message box:

Window.alert ("Hello,world");

2) Confirmation box?

Window.confirm ("Are you sure you want to delete this message?") ");

3) User input box?

Window.prompt ("Please enter the wish you wish to enter");

7. Process Control Statements

1) Select the statement.

if (logical expression) ... else

var s = window.prompt ("Please enter Age");

S=parseint (s);

if (s>=18) {

document.write ("adult");

}else{

document.write ("minors");

}

Switch...case statements

var season = window.prompt ("Please enter the season");

Switch (season) {

Case "one":

document.write ("Spring");

Break

Case "two":

document.write ("Summer");

Break

Default:

Break

}

2) Loop statement

For loop.

for (Var i=1;i<=10;i++) {

var num =i*10+ "px";

document.write ("

}

Note: Do not make a mistake with the definition of a variable in a for loop in Java.

The definition of a function in 8.JavaScript. Method

1) A method that has a return value, when defined by a method, that is the formal parameter.

function Add (A, b) {

return a+b;

}

Where A and B are formal parameters.

2) There is no way to return a value:

function out (str) {

Alert ("Output data:" +STR);

}

The elements in a Web page are associated with functions in javascript:

<input onclick= "Out (' Hello ');" type= "button" value= "Method 1"/>

JS Basic syntax

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.