javascript-basic syntax and data types

Source: Internet
Author: User

Prelude: Using JavaScript in HTML

1_ recommended src referencing external JavaScript files for ease of management and maintenance, where the tabs are located at the bottom of the page, allowing the browser to load page content more preferentially.

2_html page requires a standard document type declaration DOCTYPE, otherwise it will trigger promiscuous mode, the page is very error-prone.

3_ consider disabling JavaScript scenarios.

<body>
...
The content in the <noscript> tag is rendered when the browser does not support JavaScript or is disabled. <noscript> <p> This page requires browser support javascript.</p> </noscript> </body>

1. Basic syntax

1. Everything in ECMAScript (variables, function names, and operators) are case-sensitive.

2. Identifier Name: The first character must be a letter, an underscore or a $ dollar sign, the number cannot be in the first. (keywords and reserved words cannot be used as identifiers)

3. Strict mode "Use strict" defines a more demanding parsing and execution model

function
dosomething () { "use strict" // function Body }

2. Data type

Data types are divided into basic types and composite types.

Basic data Types : Number,boolean, String, NULL, and undifined.

Number Integer or floating-point type
Boolean Boolean type, True or false (note is case-sensitive and must be lowercase)
String Strings, single and double quotes are all possible
Null Represents an empty object pointer,
Undefined When the variable declaration is undefined, it is undefined,
Object Collections of data and features

Number Type

1. Eight binary is not valid in strict mode, the first two digits of hexadecimal must be 0x, the formula will be converted to decimal

2. Take the value range, use the global Isfinite () function to determine if it is out of range, true if it is in range, or false if it is outside the range.

Number.MAX_VALUE; /* Maximum value (JS can be represented) */ Number.min_value; /* Minimum Value (JS can be represented) */ number.negative_infinity; /* negative infinity with a value of-infinity */ number.positive_infinity; /* Infinity value is infinity */

3. Nan and isNaN ()

Nan is a number type, but not a digit, and once Nan is present in the program, it indicates a problem.

The Boolean property of Nan is False,nan and any values are not equal, including the Nan itself.

Any operation that involves Nan will return Nan.

IsNaN ()

The data inside is first converted by the number () method,

If it can be converted to a number, then isNaN () is false.

If it cannot be converted to a number, isNaN () is true.

4. Numeric conversions

    1. Number () function: To convert the data to the number type as a whole, and use the unary plus operator (+) as the same, it is recommended to use the + operator, relatively simple . Add an empty string directly.
    2. parseint () Function: Parses an integer, from left to right, encounters a non-numeric stop, can pass in data and into the system, such as parseint (' 070 ', 8) output 10 binary 56.
    3. Parsefloat () function: Resolves floating-point numbers, from left to right, recognizes the first decimal point, accepts only one parameter, and it is important to note that if the parsed data result is an integer, the integer is returned directly.

Boolean type

The Boolean type is only: true and false. Note Case-sensitive, other forms are not, such as true and false.

1. The value of any data type can be converted to its corresponding Boolean value, there are three main ways of conversion:

Display Transformations: Transformation function Boolean ()

Implicit conversions, such as conditional statements if

2. When converting to a numeric value, True and false are converted to 1 and 0, respectively, ' true ' and ' false ' when converted to a string.

True and False for the 3.Boolean type, which returns the string ' Boolean ' when using TypeOf.

String type

1. Implicit conversions, where any numeric value is added to a string, is converted to a string using string () and then combined with two strings.

2. Display the conversion string (),

3. The string is escaped with a backslash "\", and the common escape character is:

Literal quantity Meaning Literal quantity Meaning Literal quantity Meaning
\ n Line break \ t Tab \b Space
\ r Enter \f In-process \\ Back slash
\’ Single quotation marks \” Double quotes
\xnn A character represented by the hexadecimal code NN \unnnn A Unicode character represented by the hexadecimal code nnnn

null and undefined differences

Null

Undefined

Said Null Object pointer Variables that have not been defined
typeof Operation Results Object Undefined
Undefined derivation null, use = = to determine true Undefined derivation null, use = = to determine true
If you define a variable to save the object in the future,
It is better to initialize the variable to null.
It is not necessary to set a variable display to undefined, regardless of the declaration.

These five basic data types can manipulate the actual values stored in the variable directly.

The base data types are stored in the in-memory stack area.

Reference (composite) data classes : JavaScript reference data types are stored in heap memory, JavaScript does not allow direct access to the location in the heap memory space and operations heap memory space, only by manipulating the object's reference address in the stack memory.

So referencing the type of data that is stored in the stack memory is actually the object's reference address in the heap memory. This reference address allows you to quickly find objects that are stored in heap memory.

reference types are properties that can be added, Basic Types Although you can add attributes, they do not error, but they cannot be accessed .

javascript-basic syntax and data types

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.