Chapter Three: JavaScript types, values, and variables.

Source: Internet
Author: User
Tags arithmetic operators natural logarithm pow square root

The operation of a computer program needs to operate on values such as the number 3.14 or the text "Hello World", where the type of value that can be represented and manipulated is called the data type (type), and the most basic feature of a programming language is the hosting of multiple data types. When the program needs to keep the value for future use, it assigns the value to a variable (variable). A variable is a symbolic name for a value, and a reference to a value can be obtained by name. The working mechanism of variables is a basic feature of programming language. This chapter will help you understand this chapter by referring to the previous section, which will be explained in more detail later.

JavaScript data is divided into two categories: the original Class (primitive type) and the object type

The original classes in JavaScript include numbers, strings, and Booleans, and this chapter will have separate chapters devoted to JavaScript numbers, strings, and Booleans. JavaScript also has two special primitive values, null (empty) and undefined (undefined), which are not numbers, strings, or Booleans. They represent the unique members of their particular type, respectively.

JavaScript is an object in addition to numbers, strings, booleans, nulls, undefined. an object is a collection of properties. Each property consists of a "name/value pair" (the value can be a primitive value, such as a number, a string, or an object). One of the more special objects (global object) is introduced by Miss Fifth, which is described in more detail in section VI)

A generic JavaScript object is a collection of "named values" that are not required. JavaScript also defines a special object, an array, that represents an ordered set of numbered values. JavaScript defines a specialized syntax for an array. Causes the array to have some unique behavior properties that differ from ordinary objects.

JavaScript also defines a special kind of object-function. A function is an object that has the executable code that it wants to associate, runs the section execution code by invoking a function, and returns the result of the operation. As well as arrays, function behavior characteristics are different from other objects. JavaScript defines the specialized syntax for using functions. In terms of JavaScript functions. Most importantly, they are all true, and JavaScript can tell that they are treated as ordinary objects.

If a function initializes (using the new operator) A new object, we call it a constructor (constructor). Each constructor defines a class (Class) Object-a collection of constructor initialization objects. A class can be considered a subtype of an object type. In addition to array classes and function classes, JavaScript defines three other types of used classes. Date defines the object that represents the date. Regular (REGEXP) defines the object of the regular expression. The error class defines the line that represents a run-time error and syntax error object in a JavaScript program. You can define the classes you want by defining your own constructors.

The JavaScript interpreter has its own memory management mechanism that can automatically recycle memory (garbagecollection). This means that programs can create objects on demand, and programmers do not have to worry about destroying memory collections for those objects. When there is no longer any one reference to an object, the interpreter knows that the object is useless, and then automatically reclaims the memory resources it consumes.
JavaScript is an object-oriented language. Not strictly speaking, this means that we do not use the global definition function to manipulate different types of values, the data type itself can define methods (method) to work with values, such as to sort the elements in Group A, instead of having to pass a into the sort () function, rather a method of dispatching a sort ()

    // sort (a) object-oriented version

Technically, only JavaScript objects can have methods. However, numbers, strings, and Boolean values also have their own methods. In JavaScript, only null and undefined are values that cannot have a method.

JavaScript types can be categorized into primitive types and object types, and can be categorized into types that can have methods and types that cannot have methods. can also be divided into variable (mutable) and immutable (immutable) types. The value of a mutable type can be modified, and objects and arrays are mutable types: JavaScript programs can change the value of an object's property values and arrays of elements.

Numbers, Booleans, nulls, and undefined belong to immutable types. For example, modifying the contents of an array does not speak for itself. A string can be considered an array of characters, and you can think of it as being variable. In JavaScript, however, strings are immutable. You can access text anywhere in the string, but JavaScript does not provide a way to modify the contents of a string literal.

JavaScript is free to convert data types. For example, if a number is used where the program expects the string to be used, JavaScript automatically converts the number to a string. If you expect a non-Boolean value to be used where the Boolean value is used, JavaScript is converted accordingly. "Judge Equality" in JavaScript for flexible types of catch-change rules (equality)

JavaScript variables are untyped (untyped), variables can be assigned to human and type values, and the var keyword is used to declare (declare) variables. JavaScript takes a syntactic scope, and variables declared outside of any function are called global variables (variable), which are visible anywhere in JavaScript programs.

1. Digital

Unlike other programming languages, JavaScript does not differentiate between integer values and floating-point numbers. Values in JavaScript are represented by floating-point number values. When a number appears directly in the JavaScript program, we Zhiduan Chen the Digital direct amount (numeric literal), andJavaScript supports digital direct quantities in multiple formats. (Note: Adding a minus sign (-) directly before any number can give them a negative value) but the minus sign is a unary negation operator. , is not part of the digital direct volume syntax. )

I-Integer Direct volume

JavaScript uses an array sequence to represent a decimal integer

In addition to the decimal integer direct amount, JavaScript also recognizes the value of the cardinality of the 16 mechanism (16). Hexadecimal is prefixed with "0X" or "0x", followed by the direct amount of the hexadecimal number string. The hexadecimal value is a number of 0-9 and a letter consisting of a (a)-F (f). A-f Letter for the expression number 10-15 below is an example of hexadecimal integer direct volume

// 15*16+15=2550xcafe911

Although ECMAScript does not support octal direct quantities, some implementations of JavaScript can allow integers to be represented in octal (cardinality 8). The octal direct amount starts with the number 0 followed by a sequence of numbers between 0-7.

// 3*64 +7*8 +7 =255 (decimal)

Since some JavaScript implementations support the amount of octal, and some do not, it is best not to use an integer that is prefixed with 0, after all we cannot tell whether the current JavaScript implementation supports octal parsing. In the strict mode of ECMASCRIPT6, the direct amount of octal is expressly forbidden.

Ii. floating-point Direct volume

Floating-point direct quantities can contain decimal points, which are used in traditional real-number notation. A real number consists of an integer part, a decimal point and a fractional portion.

In addition, you can use exponential notation to represent floating-point direct quantities. That is, the real number is followed by the letter E or E, followed by the sign, followed by an integral type exponent. The number represented by this counting method is the exponential power with the preceding real numbers multiplied by 10.
You can use the more concise syntax to represent

[Digits] [. Digits] [(E|e) [(+|-)]digits]
            3.14            2345.455            . 33333333333333333            //6.02*10 23-time            //1.255454*10 Negative 23-time-square

Arithmetic operations in the Iii.javascript

A JavaScript program uses arithmetic operators provided by the language province to perform numeric operations. These operators contain the +-*/and remainder (evenly divisible) operator%
In addition to the basic operators, JavaScript supports more complex arithmetic operations, which are implemented by functions and constants defined as properties of the Math object.

Math.pow (2, 53)//=>9007199254740992 document.write (Math.pow (2,53))Math.Round (. 6)//=>1.0 RoundingMath.ceil (. 6)//=>1.0 up the wholeMath.floor (. 6)//=>0.0 down to the wholeMath.Abs (-5)//=>5 Absolute ValueMath.max (x, Y, z)//returns the maximum valueMath.min (x, Y, z)//returns the minimum valueMath.random ()//generates a pseudo-random number greater than 0 less than 1Math.PI//Pi PiMath.e//e: base of natural logarithmMATH.SQRT (3)//square root of 3Math.pow (3, 1/3)//Cubic root of 3Math.sin (0)//trigonometric Functions, Math.cos,math.atan, etc.Math.log (10)//=>2.302585092994046 Natural logarithm with 10 baseMath.log (+)/MATH.LN2//logarithm of 512 at base 2Math.log (+)/MATH.LN10//logarithm of 100 at base 10Math.exp (3)//three power of e

Arithmetic operations in JavaScript do not error when an overflow (overflow), underflow (underflow), or zero is divisible. However, the result of the numerical operation is more than the number of JavaScript can be represented in the line (overflow), the result is a special infinite value (infinty) value, in JavaScript with infinty representation. Similarly, when a negative value exceeds the range of negative numbers that JavaScript can express, the result is negative infinity, represented by-infinty in JavaScript. The behavior characteristics of infinite values are consistent with what we expect: The result of the subtraction operation based on them is Infinity (reserved sign)

Underflow (underflow) is a situation that occurs when the result of the operation is a wireless close to 0 and is smaller than the minimum value that JavaScript can represent. When a negative number is underflow, JavaScript returns a special value, "Minus 0", which is almost exactly the same as the normal 0 (minus 0). JavaScript programmers rarely use negative 0.

JavaScript pre-defines the global variables Infinaty and Nan, which are used to express the non-numeric values of the positive Infinity River, which can be read and written in ECMASCIPT3. ECMAScript5 fixed the problem by defining them as read-only. The property values defined by the number object in ECMASCIPT3 are also read-only, here are some examples:

Infinity//Initializes a read/write variable to InfintyNumber.POSITIVE_INFINITY//the same value, read-only1/0//and that's the same value.Number.MAX_VALUE + 1//calculate the result or infinityNumber.negative_infinity//indicates negative infinity.-Infinity-1/0-number.max_value-1NaN//initialize a read/write variable to nanNumber.NaN//same value, but read-only0/0//The result of the calculation or Nan NUMBER.MIN_VALUE/2//underflow occurred. Calculated as 0-NUMBER.MIN_VALUE/2//Negative 0 -1/infinity//Negative 0-0//Negative 0

a non-numeric value in JavaScript is a bit special, and it is not equal to people and values, including itself. That is, it is impossible to determine if X is Nan by X==nan. Instead, the x! should be used =x to judge that the result of an expression is true if and only if X is Nan. function isNaN () is similar to this, and returns true if the parameter is Nan or is a non-numeric value (such as a string and an object). JavaScript has a similar function, Isfinite (), that returns True when the parameter is not Nan, infinty, or-infinity.

The negative 0 value is also somewhat special, it is equal to plus or minus 0 (even judged by the strict equivalence test of JavaScript), which means that the two values are almost identical, except as a divisor:

        var zero = 0;         var negz =-0;         // =>true plus or minus 0 values equal        // false positive infinity and negative infinity range

IIII. Binary floating-point numbers and rounding errors

(not to be continued)

Chapter Three: JavaScript types, values, and 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.