Basics of JavaScript

Source: Internet
Author: User

1, identifier

An identifier is a term called a constant or variable named in a program, and not all character composition is a valid identifier, as follows:

    1. The component of an identifier can be a letter, a number, an underscore, or a dollar ($) symbol
    2. The identifier begins with a letter, underscore, or dollar ($) symbol, and cannot be a number
    3. Identifiers cannot have special symbols (such as: ¥,%,+) and cannot have spaces
    4. Cannot be a reserved word (that is, a keyword) in JavaScript, such as: Var,new,if,else, etc.
    5. Identifiers are case-sensitive, myname and myname are two different identifiers

The following identifiers are correct:

Mypencil myName _myage $mySex xyz123 _123abc Var New

The following identifiers are incorrect :

123abc My+name my,. Age Var New

Data types in 2.JavaScript

    1. Numeric type (number)

Integral type

integer data, which is what we usually call integers, such as:1, and so on.

Integer data can be decimal, octal, and 16, with the default decimal

If you need to represent octal data, you must start with the number 0 . such as octal 037, its decimal value is .

If you need to represent hexadecimal data, you must start with 0x or 0X , where 0 is the number zero. such as the hexadecimal 0x3d, its decimal value is .

Floating point Type

Floating-point data, which is what we usually call decimals. such as:3.14,12.34,100.5 and so on.

Real data can be represented by using scientific notation in addition to the common format. such as:8.2 8 - Time Square-8E+8;8.2 's negative 8 Second Party--8.2E-8.

Special values

Infinity: When a positive floating-point data is greater than or equal to the maximum value it can represent (2 of the Cubic), The result is a special positive infinity,which is identified byJavaScript as Infinity.

- Infinity : when a negative floating-point data is less than or equal to the minimum value it can represent (2 of the time), the result is a special negative infinity,JavaScript Identify it as -Infinity.

NaN : that Not a number, which indicates that a value is not a valid numeric form, such as a string (except for a pure numeric string). This value is special, it is not equal to any numeric value, including itself, so we are specifically using the function IsNaN () to detect this!

2. String type

There is no separate character constant in JavaScript, but a string constant with several characters

String constants must enclose each constituent character in single quotation marks ("') or double quotation marks (" "), such as" AB "," Abook "

When a string does not include any characters, its form is "" or "" means an empty string

In JavaScript, some special symbols cannot be output directly, and in this case a special character constant is required to represent the escape character

The escape character must begin with a backslash (\) followed by one or several specific characters

Escape character

Unicode Code

Meaning

\b

\u0008

Backspace

\ t

\u0009

Jumps horizontally to the next tab position

\ n

\u000a

Carriage return line break

\v

\u000b

Jump vertically to the next tab position

\f

\u000c

Go paper To change pages

\ r

\u000d

Enter

\”

\u0022

Double quotes

\’

\u0027

Single quotation marks

\\

\u005c

Back slash

\xnn

The hexadecimal code NN (00~ff) represents the character.

\unnnn

The hexadecimal code nnnn (0000~ffff) represents the Unicode encoding.

\x3c

Character "<"

\x3e

Character ">"

3, Boolean type (Boolean)

Boolean values are used to denote the positive and negative side of a thing

This type is only true and false, and is typically used for conditional judgment statements

4, null-value (NULL)

This is a value, NULL, which indicates that the object to which a variable points is a null value!

it means "no value" and does not represent"null", which is 4 letters or 0 and an empty string, but An object type in JavaScript

null can be used to initialize a variable to avoid an error or to clear the contents of a variable, freeing the memory space associated with the variable. When NULL is assigned to a variable, no valid data is saved in this variable!

5, undefined type (undefined)

This is a value, undefined, that indicates that the variable is not declared or uninitialized or that a property of the object does not exist.

Null means that the value assigned to the variable is "null", "NULL" is a special value, while undefined indicates that the variable is not present or uninitialized, and the value of the variable is still in an unknown state!

When using the equals operator "= =",both null and undefined are equal to themselves and are equal to each other, but not equal to any other values.

The strict equals operator "= = =" considers null and undefined values unequal.

Conversion of data types in 3,javascript

Implicit conversions

The numeric string is added to the numeric value, the string is concatenated, and the final result is a string type

"123" +123 = "123123"

The numeric string is subtracted from the numeric value, the arithmetic subtraction is performed, and the final result is the number type

"123"-123 = 0

String with any other type of data "+" operation, the string of the connection operation, the final result is a string type

null+ "123" +undefined+ "abc" +123+ "FGH" +true = "Null123undefinedabc123fghtrue"

True with the value to add and subtract, will automatically convert to 1, and then participate in the operation, the final result is number type!

true+true+1 = 3

False and the value are added and minus, automatically converted to 0 and then participate in the operation, the final result is number type!

true+false+1=2

Null and null,null and numeric are added minus, andnull values are always 0. The final result is number!

null+null+1 = 1

Undefined and values are added minus, and the final result is always Nan

null+undefined+true

Basic Conversions

String ()

Convert a value of another type to a string type  

String (280) + string (true) = "280true"

Number ()

Convert other types that can be converted to numbers to number types

Number ("123") +number (true) +number (false) +number (null) = 124

Boolean ()

Convert other types of values to a Boolean type! In addition to 0,null,undefined,NaN,"" the result of the conversion is false , the other values are converted to true.

String ("123" +boolean (0) +boolean (null) +boolean ("ab")) = "123falsefalsetrue"

Extract Transformation

parseint (string,n)

This function converts the substring of the string string parameter that is first converted to an integer to a numeric value!

          where the parametersNused to specify what the string parameter will be converted to, which can be2,8,Ten, -as well2~32If you do not specify this parameter, the default value isTenbinary to convert, the converted values will be in decimal form! (Note: When you do not specifyNargument, if the first character of the string is a number0, you will follow8binary to convert! )
whenStringwhen the string header does not have a number substring, the function eventually returnsnan!

parseint ("100.23abc") =parseint ("+100.23abc") =parseint ("-100.23abc") = -100parseint (" 0100.23abc ") = (0100) 8 =parseint ("100.23abc ", 2) = (+) 2 = 4parseint ("100.23abc ", 16) = (100) 16 =parseint ("100.23abc", 8) = (100) 8 = 64

Parsefloat (String)

The function converts the string that is the first in a string string argument to a numeric value

When a string string is not present, the function eventually returns Nan

Parsefloat ("123.23abc") = 123.23parsefloat ("+123.23abc") = 123.23parsefloat ("-123.23abc") =- 123.23parsefloat ("123.23.34ab") = 123.23parsefloat (". 123.23.34ab ") = 0.123parsefloat ("0123.adf ") = 123

Eval (string_expression)

The function evaluates the value of the string expression and returns

if the string is not a valid expression, an exception will occur, and we can use the Try...catch statement to determine the occurrence of this situation!

Eval ("3+3+3+3+3*10") = eval ("3+3+4+ (3+2) *10") =eval("trueeval" ("3++34-2")              // will throw an exception! 

Basics of JavaScript

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.