JavaScript Programming Start (second lesson) _ Basics

Source: Internet
Author: User
Tags arithmetic arithmetic operators numeric value
Today we mainly study the following pieces of content:
1. Java Script variable
2. Java Script expressions and operators

Variables can be declared with Var plus the name specified for the variable, and the variable type can be determined by assigning a value to the variable. Because Java script uses a weakly typed style, it is less stringent on data types, and is automatically converted as needed during program execution.
For a string variable, you can get the length of the string in the variable by "variable name. Length", such as
var name;
Name= "java Script";
Then the Name.length value is 10.

If you create multiple variables in a row, remember to separate the variable names with commas. The statements are separated by semicolons. (The use of semicolons is a good habit, we learn to try to develop the habit of adding semicolons)

Type conversions: Java script allows you to change the type of variables in your program, the most common two type conversions, number, and string.
Number (x) is a character-value--〉 numeric value type. string is the opposite. This conversion can be a mandatory type conversion, relative to the automatic type conversion of Java script. (Mandatory type conversions are required in the Java script1.2 and above versions)

Name of variable:

1. You must begin with a letter or an underscore, with alphanumeric and/or underlining in the middle. You cannot use spaces, +,-and other symbols.
As a hyphen, the variable name cannot have spaces, (+), (-), (,), or other symbols.
2. You cannot use a keyword in javascript as a variable.
(Java script variable names are case-sensitive, and name and name are not the same.) )

There is another importance to a variable-that is, the scope of the variable. There are also global variables and local variables in JavaScript. Global variables are defined outside the body of all functions and are scoped to the entire function, while local variables are defined within the function body and are visible only to the function, but not to other functions.

Example:
<script>


Expressions: After you have defined variables, you can assign them, a series of operations, such as change, calculation, and so on, which is usually done by an expression, which can be said to be a set of variables, constants, Boolean, and operators, so that the expression can be divided into arithmetic expressions, string expressions, assignment expressions, and Boolean expressions.


Operator:

1. Arithmetic operators: + (plus),-(minus), * (multiply),/(except),% (modulo)-(take Back),
+ + (accumulator 1) 、--(descending 1).
Example: 11%2=1; if x=2 ++x+4=7 x+++4=6
(++x is the first to execute plus 1,x++ is executed after the statement x is added 1
Example:

<script>

2. Comparison operator:< (less than), > (greater than), <= (less than equal), >= (greater than equal), = = (equals),!= (not equal)
(the basic procedure is to compare its operands first, and then return a value of TRUE or false.) )
3. Logical operator:! (inverse), &= (assigned after), & (Logical AND), |= (or later assigned), | (logical OR),
^= (assign value after or after), ^ (logical xor),?: (three-eye operator), | | (or), && (with)
= = = (equal to), |= (not equal to).
4. String operator: Only + ("my" + "java Script" result equals "my Java Script")
5. Assignment operator: That is =, assigns the value on the right to the variable on the left.
6. Conditional operator: (? :)
Example: status= (age>=18)? " Adult ": child"; if greater than 18, the value of the expression is adult.
7.typeof () Operator: the type used to return a variable or data.
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.