Basic use of JavaScript summary ①

Source: Internet
Author: User
Tags bitwise operators object object script tag

JavaScript uses

JavaScript is a lightweight programming language, and JavaScript is a programming code that can be inserted into an HTML page, and JavaScript is inserted into the HTML page and can be performed by all modern browsers.

The script in the HTML must be between the <script> and </script> tags. The script can be placed in the <body> and

Three ways to use JS
1. Using embedded JS directly in HTML tags is not recommended
does not conform to the requirements of the content and behavior separation.

2. Use the script tag to wrap the JS code in the HTML page.
The script tag can be placed anywhere on the page

3. Introduction of external JS file, using script tag

JS statement


JavaScript If ... Else Statement

    • If statement-use this statement to execute code only if the specified condition is true
    • If...else Statement-executes code when the condition is true and executes other code when the condition is false
    • If...else If....else Statement-Use this statement to select one of several code blocks to execute
    • Switch statement-Use this statement to select one of several code blocks to execute
    • JavaScript Switch Statement

      Use the switch statement to select one of several code blocks to execute

How it works: first set an expression n (usually a variable). The value of the subsequent expression is compared to the value of each case in the structure. If there is a match, the code block associated with the case is executed. Use break to prevent your code from automatically running down a case.

Switch (n) {Case 1:    break;case 2:    break;default:}

JavaScript for Loop

If you want to run the same code over and over again, and the values are different each time, it is convenient to use loops.

document.write (Cars[0] + "<br>");d Ocument.write (cars[1] + "<br>");d Ocument.write (cars[2] + "<br>") ;d Ocument.write (cars[3] + "<br>");d Ocument.write (cars[4] + "<br>");d Ocument.write (cars[5] + "<br>" );

JavaScript While Loop

Loops can execute code all the time, as long as the specified condition is true.

While loop

The while loop executes a code block when the specified condition is true.

Grammar
while (condition)  {  code to execute  }

JavaScript Break and Continue statements

The break statement is used to jump out of a loop.

The Continue is used to skip an iteration in the loop.

Break statement

We have seen break statements in earlier chapters of this tutorial. It is used to jump out of a switch () statement.

The break statement can be used to jump out of a loop.

After the break statement jumps out of the loop, the code after the loop resumes (if any):

Instance
for (i=0;i<10;i++)  {  if (i==3)    {    break;    }  x=x + "The number is" + i + "<br>";  }



JavaScript Variables

1. As with algebra, JavaScript variables can be used to hold values (such as x=2) and expressions (such as z=x+y).

Variables can use short names (such as x and y), or they can use better descriptive names (such as age, Sum, totalvolume).


2. Naming requirements for variable names
① variable names can only be composed of alphanumeric underscores.
② cannot start with a number.
③ variable names are case-sensitive, and uppercase and lowercase letters are different variables. (Plain capital letters are generally used to denote constants)


3. Naming conventions for variable names
① conforms to the small hump rule (hump rule) Multiple words consist of variable names, with the exception of the first letter lowercase and the first letter of each word capitalized.
Mynameisli
② or use Hungarian nomenclature, all letters lowercase, and the words are separated by an underscore _.



4. Data types in JS (remember)
The ①undefind is not assigned, and the variable declared with VAR has been used but is not assigned a value.
②nall represents an empty reference
The ③boolean boolean type. True Ture, Flase.
④number numeric type, can be an integer, can be a decimal,
⑤string string type, with "" or "package
⑥object Object Type

JavaScript Data Types

Use the VAR keyword to declare the variable:

var carname;

After the variable declaration, the variable is empty (it has no value).

To assign a value to a variable, use the equals sign:

Carname= "Volvo";

However, you can also assign a value to a variable when you declare it:

var carname= "Volvo";

JavaScript variables can also hold other data types, such as text values (name= "Bill Gates").

In JavaScript, a text like "Bill Gates" is called a string.

There are many types of JavaScript variables, but for now, we only focus on numbers and strings.

When you assign a text value to a variable, enclose the value in double or single quotation marks.

Do not use quotation marks when the value you assign to a variable is numeric. If you enclose a value in quotation marks, the value is treated as text.

JavaScript Operators

Arithmetic operations (single-mesh operators)
+,-, *,/(reserved decimals),%, + + self-increment,--auto minus
+ addition operation, connection string, except add extra, the remaining symbols will first try to turn the left and right side number

[a++/++a similarities and differences], (--the same)

A=1 b= a++ +2 >>>b=3 a=2 First performs the addition with the value of a, and then adds a.
c= ++a +2 >>>5 perform a self-increment first, then add

Assignment operations
= += -= *= /= %=
=: assigned value;
+=:a+=b; equivalent to a=a+b;

Relational operations
= =,! =, >, <, >=, <=
The result of the relational operator operation is the Boolean type Ture/flase.
"To determine whether a number is in a certain range must be used && link.
= = equals, the type is the same direct judgment, different types will try to convert both sides to number and then judge.
! = Not equal to
= = = Strictly equal to the value type must be the same, the type of the result is false, the same type and then the next judgment.
! = = Not strictly equal to

Conditional operator (multi-mesh operation)
A>b?true:false

Have two important symbols? And:
When the part of the question mark before the result is ture execution: The preceding code, flase execution: The following code: The two sides can be numeric, the entire formula can be used to assign a value. Both sides of the colon are code blocks, and code is executed directly.
Multi-mesh operations can be nested in multiple layers

Bitwise operators, logical operators
&&, | |,!


Precedence of Operators
()
! ++ --
* / %
+ -
> < >= <=
== !=
&& and/or simultaneous presence vs. or high
||
= + = *=/= The lowest is a variety of assignments

Basic use of JavaScript 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.