JavaScript Foundation Hardening 2--Syntax 1

Source: Internet
Author: User
Tags arithmetic operators

2.1 Variable 2.1.1 What is a variable

Literally, variables are variable quantities, and from a programmatic point of view, variables are memory used to store data, each with its own unique name and each variable occupying a memory.

In the program, it is not convenient to use the data value directly or the memory address of the data value directly, so we use the name of the variable to represent the corresponding data.

Each variable has its variable name, variable type, and scope of the variable.

2.1.2 A variable in JavaScript

The variable rules in JavaScript are similar to other languages such as C, Java, and the recommended variable naming conventions:

    • 1. Composed of letters, numbers, underscores, case-sensitive
    • 2. Start with a letter
    • 3. Variable names cannot have spaces
    • 4. Do not use keywords in JavaScript for variable names
Declaration of the 2.1.3 variable

In JavaScript, variables are declared with the Var command:

var test;    // A variable named test was declared.  var test_2 = "Shiyanlou";  // declare a variable named test_2 and assign a value of "Shiyanlou". 

In JavaScript, variables can also be declared without the use of a variable, depending on the type of data, such as:

x = +;     // variable x is an integer // variable y is a string True ;    // variable z is a Boolean type // cars is an array
2.1.4 Scope

As in other languages, the variables in JavaScript also have global variables and local variables.

Global variables are defined outside of all functions and are scoped to the entire JavaScript code;

A local variable is defined within the body of a function and is visible only to its function, but not to other functions.

2.2 Data Type 2.2.1 String

A string is a variable that stores characters (such as "Shiyanlou").

Strings can be any text in quotation marks, you can use either single or double quotation marks, or you can use quotation marks in a string, as long as you do not match the quotation marks surrounding the string:

var carname= "Shiyanlou"; var carname= ' Shiyanlou '; var answer= "I love ' Shiyanlou '"; var answer= ' I love ' Shiyanlou ';
2.2.2 Digital

JavaScript has only one numeric type. Numbers can be with decimal points or without:

var x1=34.00;      // use a decimal point to write var x2=34;         // do not use the decimal point to write
2.2.3 Boolean

Boolean can have only two values: TRUE or false:

var x=truevar y=false
2.2.4 Array

Create an array named Boys:

var boys=NewArray(); boys[0]= "Tom"; boys[1]= "Jack"; boys[2]= "Alex";

You can also do this:

var boys=NewArray("Tom", "Jack", "Alex");
2.3 operator

Most programming languages have similar operator rules, and JavaScript is close to what most people know about C, Java, and so on.

2.3.1 Arithmetic operators
operator Description Example
+ Add X+y
- Reducing X-y
* By X*y
/ Except X/Y
++ Accumulation X + +
-- Fatigue reduction x--
% Take the remainder X%y
2.3.2 comparison Operators

The basic operation of the comparison operator is to compare its operands first, and then return a value of true or False, with 8 comparison operators:

< (less than), > (greater than), <= (less than or equal), >= (greater than or equal), = = (equals),! = (not equal).

JavaScript is a weakly typed (or dynamically typed) language in which the type of the variable is indeterminate, understanding the following four sections of JavaScript code, running and observing, exploring the uncertainties of variable types:

x=5+5;     // Digital + Digital document. write (x); y // string + string document. write (y); M=7+ "7";  // number + string document. write (m); n= "8" +8;  // string + digital document.write (n);

JavaScript Foundation Hardening 2--Syntax 1

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.