JS Basic syntax

Source: Internet
Author: User
Tags arithmetic operators

1) semicolons are used to split the JS statement. You can wrap the semicolon after it, or you can write the next line of statements.

2) JS code is the sequence of JS statements, the browser will be executed in order.

3) JS statements are combined in the form of code blocks. The block starts with the left curly brace and ends with the closing curly brace. The function of a block is to make the statement sequence execute together. The JS function is a typical example of combining statements in blocks.

4) JS is sensitive to case.

5) JS will ignore the extra spaces. You can add spaces to the script to improve its readability.

6) JS Code allows you to wrap lines of code in a text string using a backslash.

document.write ("Hello world!");

7) JS Comment

Single-line comments begin with//. (also available for end of line)

Multiline comments start with/* and end with */.

8) JS Variable

JS variables can use short names (such as x and y), or they can use better descriptive names (such as age, Sum, totalvolume). Variables must start with a letter and are case sensitive.

//     JS declares variables through Var and can be assigned at the same time when declaring.  var pi=3.14; var name= "Bill Gates"; var answer= ' Yes I am! ' ; //     You can also declare many variables in a statement var name= "Gates", age=56, job= "CEO"; //     Declare a undefined variable var carname;

9) JS data type

//JS has a dynamic typevarX//x is undefinedvarx = 6;//x is a numbervarx = "Bill";//x is a string//JavaScript has only one numeric type. Numbers can be taken with decimal points or without. varx1=34.00;//use a decimal point to writevarx2=34;//do not use the decimal point to write//Boolean (logic) can have only two values: TRUE or FALSE. varx=truevary=false//The following code creates an array named carsvarcars=NewArray ("Audi", "BMW", "Volvo");//objects are delimited by curly braces, and the properties of an object are defined in the form of name and value pairs (name:value)varPerson={firstname: "Bill", LastName: "Gates", id:5566};//you can empty a variable by setting the value of the variable to null. person=NULL;//when declaring a new variable, you can use the keyword "new" to declare its typevarCarname=NewString;varx=NewNumber ;vary=NewBoolean;varcars=NewArray;varperson=NewObject;

) JS Object

All things in JavaScript are objects: strings, numbers, arrays, dates, and so on.

In JavaScript, objects are data that owns properties and methods.

You can also create your own objects.

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

One) JS function

A function is an event-driven or reusable block of code that executes when it is invoked. A function is a block of code wrapped in curly braces, preceded by a keyword function:

function functionname () {This is the code to execute}

When the function is called, the code inside the function is executed.

A function can be called directly when an event occurs (such as when a user taps a button) and can be called from anywhere by JavaScript.

When you call a function, you can pass a value to it, which is called a parameter.

MyFunction (Argument1,argument2)

When you declare a function, declare the argument as a variable:

function myFunction (VAR1,VAR2) {This is the code to execute}

Variables and parameters must appear in a consistent order. The first variable is the given value of the first parameter passed, and so on.

<button onclick= "myFunction (' Bill Gates ', ' CEO ')" > click here </button><script>function  MyFunction (name,job) {alert ("Welcome" + name + ", the" + Job);} </script>

If a function has a return statement, it is a function with a return value.

function MyFunction (A, a) {return A *b;} document.getElementById ("Demo"). Innerhtml=myfunction (4,3);

A variable declared inside a JavaScript function (using VAR) is a local variable, so it can only be accessed inside the function. (The scope of the variable is local).

You can use local variables with the same name in different functions, because only the function that declares the variable can recognize the variable.

Variables declared outside of a function are global variables, and all scripts and functions on a Web page can access it.

The lifetime of JavaScript variables begins at the time they are declared. Local variables are deleted after the function is run. Global variables are deleted after the page is closed.

) JS operator

Arithmetic operators: +-*/% + +--

Comparison operators: = = = = = = = = = > < >= <=

Logical operator:&& | | !

Conditional operators:

// condition is true, assign value value1, otherwise assign value2

JS's conditional statement

If statement:

if (Condition 1)  {  true  when executing code  }Elseif (condition 2)  {  True code executed at the   time of the code}else  {  true   }

Switch statement:

Switch (n) { case 1:  1 break  ;  Case 2:  2 break  ; default :    Case   code executed at different time in case 2}

For statement:

 for (statement 1; Statement 2; Statement 3)  {The  code block executed  }// statement 1 executes before the Loop (code block) starts, Statement 2 defines the condition that runs the Loop (code block), and statement 3 executes after the Loop (code block) has been executed

For/in Loop statement:

var person={fname: "John", lname: "Doe", Age:25};  for inch Person )  {  txt=txt + person[x];  }

While Loop statement:

// First Judge the recycling  while (condition)  {  code to execute  }//  loop before judging   do {code to execute  }  while (conditions);

Break is used to jump out of a loop. The Continue is used to skip an iteration in the loop.

With the label reference, the break statement can be used to jump out of any JS block of code.

cars=["BMW", "Volvo", "Saab", "Ford"];list:{document.write (cars[0] + "<br>");d Ocument.write (Cars [1] + "<br>");d ocument.write (cars[2] + "<br>");  Break List;document.write (cars[3] + "<br>");d ocument.write (cars[4] + "<br>"); document.write (cars[5] + "<br>");}

14)

typeof, the data type of the detection variable

NULL, which represents an empty object reference

Undefined, is a variable with no value set

15) Type Conversion

JS has 5 different data types: string,number,boolean,object,function

JS has 3 different object types: object,array,date

JS has 2 types of data that do not contain any values: null,undefined

The constructor property returns the constructors for all JS variables.

Type conversion function: String (), toString (), number (), Date (), GetTime ().

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.