Basic JavaScript Concepts (i)

Source: Internet
Author: User

JavaScript Basic Syntax:

ECMAScript's syntax draws heavily on the syntax of C and other C-languages.

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

Identifier:

Meaning: The name of a variable, function, property, or parameter of a function.

The rules are as follows:

1. The first character must be a letter, an underscore, or a dollar sign ($);

2. Other characters can be letters, underscores, dollar signs, or numbers.

By convention: The ECMAScript identifier is in hump case format, which is the first letter lowercase, with the first letter of each word remaining capitalized.

You cannot use keywords, reserved words, true, false, and NULL as identifiers.

Comments:

Single line://

Multiple lines:/* */

Strict mode:

ECMAScript5 introduced the strict mode (strict mode), and the strict pattern is that JavaScript defines a different parsing and execution pattern. In strict mode, some of the indeterminate behavior in ECMASCRIPT3 will be handled and an error will be thrown for some unsafe operations. To enable strict mode throughout the script, you can add the following code at the top:

"Use strict";

It looks like a string, in fact it is a compilation indicator (pragma) that tells the supported JavaScript engine to switch to strict mode.

Strict mode can also be used inside the function:

Function dosomething () {

"Use strict";

function body

}

The results of JavaScript execution in strict mode can vary greatly.

Statement:

The statement in ECMAScript ends with a semicolon, and if the semicolon is omitted, the parser determines the end of the statement.

Keywords and reserved words:

The reserved keyword for Javascript cannot be used as a variable, tag, or function name. Some reserved keywords are used as Javascript extensions later.

Abstract

Arguments

Boolean

Break

Byte

Case

Catch

Char

class*

Const

Continue

Debugger

Default

Delete

Do

Double

Else

enum*

Eval

export*

extends*

False

Final

Finally

Float

For

function

Goto

If

Implements

import*

Inch

instanceof

Int

Interface

Let

Long

Native

New

Null

Package

Private

Protected

Public

Return

Short

Static

super*

Switch

Synchronized

This

Throw

Throws

Transient

True

Try

typeof

Var

void

Volatile

While

With

Yield

* The keyword of the tag is newly added in ECMAScript5.

JavaScript objects, properties, and methods

You should also avoid using JavaScript's built-in object, properties, and method names as a variable or function name for javascript:

Array

Date

Eval

function

hasOwnProperty

Infinity

Isfinite

IsNaN

isPrototypeOf

Length

Math

NaN

Name

Number

Object

Prototype

String

Tostring

Undefined

ValueOf

Variable:

ECMAScript variables are loosely typed and can be used to hold any type of data. Each variable is just a placeholder for holding the value.

Define variable: var variable name; An uninitialized variable like this will save a special value-undefined

A variable defined with the var operator becomes a local variable in the scope that defines the variable. That is, if you use var to define a variable in a function, it will be destroyed after the function exits, and the variable will be changed to a global variable if the variable is defined in the function, so it is not recommended.

You can define multiple variables with a single statement, separated by commas.

Data type:

There are five simple data types (also known as basic data types) in ECMAScript: Undefined, Null, Boolean, number, String. There is also a complex data type---Object, which is essentially composed of a set of unordered name-value pairs. The ECMAScript data type is dynamic, so defining these types is sufficient.

Typeof Operator:

Purpose: Detects the data type of a given variable.

"Undefined" not defined

Boolean value "Boolean"

String, "string"

numeric value, "number"

Objects, object, or null

Functions, function

typeof is an operator and not a function.

(from a technical point of view, a function is an object in ECMAScript, not a data type.) However, functions have some special properties, so it is necessary to distinguish functions and other objects by typeof operators.

Undefined :

This type has only one value, that is, a special undefined. When you declare a variable with VAR but do not initialize it, the value of this variable is undefined.

Null:

It is the second data type with only one value, and this special value is null. From a logical point of view, a null value represents an empty object pointer, which is why typeof returns object when NULL is detected.

If the defined variable is prepared to save the object in the future, it is better to initialize the variable to null instead of the other value, so as long as you check the null value directly, you know whether the corresponding variable has saved a reference to the object. In fact, the undefined values are derived from null, and they always return true with the equality operator (= =), but this operator is converted to other operands for comparison purposes. Although there is such a relationship between them, their use is completely different.

Boolean :

This type has only two literal values: True/false. These two values are not the same as numeric values and are case-sensitive.

Although it has a value of only two, the value of all types in ECMAScript is equivalent to these two Boolean values. To convert a value to its corresponding Boolean value, you can call the Transform function Boolean ().

0 and nan

TD valign= "Top" width= "184" >

Data type

Convert to True

Converted to false

Boolean

True

False

string

Any string

" "Empty string

number

Any non 0 value

Object

Any object

Null

Undefined

N/a

Undefined

Number:

It uses the IEEE754 format to represent integers and floating point values (floating-point numbers are also known as double-precision values in some languages). To support various numeric types, ECMA-262 defines different numeric literal formats.

The most basic numeric literal format is a decimal integer, in addition to the decimal, there are eight binary (the first bit must be 0, then the number sequence is 0~7, if the value of the literal values out of range, then the leading 0 will be ignored, the subsequent number is treated as a decimal value parsing, in strict mode octal literal value is invalid, Will cause the JS engine to throw an error) and Hex (the front two bits must be 0x, followed by the hexadecimal digit 0~9,a~f (or a~f)). The calculations are converted to decimal in numerical calculations.

Given the way JS stores values, you can save positive 0 (+0) and negative 0 (-0). Positive zeros and negative 0 are considered equal.

Floating point value:

The so-called floating-point value is that the value must contain a decimal point, and must have at least one digit after the decimal.

Because the memory space required to hold a floating-point value is twice times the integer value, ECMAScript will lose no chance to convert the floating-point number to an integer value. If there is no number after the decimal point will be the most integer value to save. Similarly, if the floating-point value itself represents an integer, the value is also converted to an integer.

For those values with a maximum or minimum value, a floating-point value represented by the E (or e) notation (i.e. scientific notation) can be used. The highest precision for floating-point values is 17 decimal places. (Note: floating-point numerical calculations produce errors, which is a common problem with IEEE754 numeric floating-point calculations.)

Range of values:

Due to memory limitations, ECMAScript cannot save all the values in the world. The minimum value is guaranteed: Number.MIN.VALUE, the value is 5e-324, the maximum value is guaranteed: Number.MAX.VALUE, the value is 1.7976931348623157e+308. If the result of the calculation exceeds that range, it is automatically converted to a special Infinity value, if negative:-infinity (negative infinity), or Infinity (positive infinity) if it is a positive number.

If a calculation returns a infinity value, the following calculation cannot be performed because infinity is not a numeric value that can participate in the calculation. You can use the Isfinite () function if you want to determine whether a number is poor (that is, between the minimum and maximum values). The function parameter is returned true between the minimum and maximum values.

NaN:

Nan is a non-numeric value (not a number) is a special value that represents a case where the operand that would have returned a numeric value does not return a number (so that no error is thrown). In ECMAScript, any number divided by a non-numeric value returns NAN, without affecting the execution of other code. (Note: 0 is actually divided by 0 to return nan, Integer divided by 0 to return infinitiy, negative number divided by 0 to return-infinitiy)

Two features:1> any operation involving Nan will return Nan.

The 2>nan is not equal to any values, including the Nan itself.

Based on the above two features, ECMAScript defines the isNaN () function, which takes a parameter that can be any type, and this function helps us determine whether the parameter is not a numeric value. IsNaN () will attempt to convert the value to a numeric value after it receives one. Some values that are not numeric are converted directly to numbers.

IsNaN () also applies to objects. When the isNaN () function is called based on an object, the function is called first.

(not to be continued ...) )

Basic JavaScript Concepts (i)

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.