Kidney Day Arch A Stroke JavaScript series Chapter2 Values&variables

Source: Internet
Author: User

Chapter 2 values and variables

1. Value values

The only thing a computer can do is to process data.

All the data processed by the computer, whether digital, text, or pictures, audio and video, eventually run in the hardware in the form of bit, mathematically expressed as 0 and 1, in a physical way to express the current low and high pressure.

In a programming language, data is referred to as a "value". The value is made up of a series of bits.

2. Type of value Types

There are altogether six types of JavaScript values, including five basic types: number,string,boolean,null,undefined and a complex type: object.

You can use the TypeOf operator to detect data types. typeof returns one of the following values: Undefined,boolean,number,string,object,function

2.1 Number

JavaScript uses 64 bits to represent a number, which can represent up to 264 cases, so the number that can be stored is limited. Because some bits are used to denote a minus sign and a decimal point, fewer than 264 are actually stored.

Since decimals can only be decimals of finite digits, decimal operations tend to lose some precision, for example, the result of 0.1+0.2 is not equal to 0.3.

In addition to the usual notation, scientific notation can be used, such as 3.14e12.

In addition to decimal, you can also use octal and hexadecimal.

Three special numbers: Infinity,-infinity, NaN

The maximum and minimum numbers are stored in Number.MAX_VALUE and Number.min_value, and if a value exceeds this range, it is automatically converted to Infinity or-infinity.

A value of Nan is designed to stop the program from throwing an error when it should have calculated the value but not the value.

Any operation that has a Nan participation will return Nan;nan and any number are unequal, including itself.

IsNaN (value) is used to detect whether value is a non-numeric value. Returns False if value is a numeric value or can be converted to a numeric value, or vice versa, which indicates "not a numeric value."

There are three functions that can convert a non-numeric value to a numeric value: Number (), parseint (), and parsefloat ()

2.2 String

strings are enclosed in single or double quotation marks.

The "/" escape character is used to resolve conflicts with certain character parsing.

"+" for the connection string.

2.3 Boolean

A Boolean type has only two values: true and false;

All of the values in JavaScript can be automatically converted to Boolean values by the system when needed, as follows:

Except for false, NULL, "", 0, NaN, and undefined are false, the other values are true.

This is the translation mechanism that JavaScript contains.

2.4 Undefined

Undefined is a global variable that has only one value that is undefined, indicating that it has been declared but not initialized (assigned value). Literally, we seem to be able to derive a relationship: Define = declare + initialize.

2.5 NULL

NULL means "null" or "Non-object". The jargon is called "null object Pointers".

When declaring a variable that will be used to hold an object, it is best to initialize it with NULL.

typeof NULL = = object//True

undefined = = NULL//True

undefined = = = NULL//False

2.6 Object

Formally,an object is a group of pairs of name and value.

The difference between an object and the first five types of data is that there is a fundamental difference in how it is stored.

There are two ways to store values: Stack and heap (heap), the data stored in the stack is called the base type, and the data stored in the heap is referred to as the reference type.

The first five kinds of data are basic types (Primitive, and primitive types), they are stored in the stack memory, have a fixed position and size, their values are immutable (immutable), access to the basic data type is by value access.

objects are reference types (Reference), they are stored in heap memory, have no fixed size, the value of the object is variable (mutable), and access to the object is accessed by reference.

The difference between heap and stack stems from the need to minimize memory footprint.

Another important difference is that the object in ECMA-262 is different from the host object (BOM and DOM), and the properties and methods of the host object are provided by the host.

3. Type conversion converision

Type conversions are divided into implicit and explicit two ways.

An implicit conversion is a program that automatically converts certain values to the desired type as needed when it is run. For example, when the comparison operation, the addition and subtraction operations and the condition of the IF statement are judged.

An explicit conversion is the conversion of a value by a function. For example, the global ToString (), ValueOf (), parseint (), and parsefloat () methods.

4. Variable Variables

All identifiers, except for words with specific functions, are almost always variables.

A variable does not "store" a value, a variable is simply a name, a reference to a value (reference), or a map (indicator). A variable is literally just a name for a particular value at a particular time.

The keyword VAR declares a variable as a local variable of its scope, and if you do not use VAR, the variable that is created is a global variable. A variable declared in a function is created and assigned only when the function is called, and is immediately destroyed when the function call ends.

Declaring variables can be written like this:

var a = value,

b = value,

c = value;

The scope of the variable is explained in the function chapter.

Kidney Day Arch A Stroke JavaScript series Chapter2 Values&variables

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.