Javascriptav data types and variables

Source: Internet
Author: User

data Type a computer is a machine that can do mathematical calculations as the name implies, so that computer programs can handle a variety of values. However, the computer can handle far more than the numerical value, but also can deal with text, graphics, audio, video, web and other kinds of data, different data, need to define different data types. The following data types are defined in javascript: Numberjavascript does not distinguish between integers and floating-point numbers, and is represented by number, and the following are valid number types:123;//Integer 1230.456;//floating point 0.4561.2345e3;//scientific notation means 1.2345x1000, equivalent to 1234.5-99;//Negative numberNaN;//Nan means not a number, which is represented by Nan when the result cannot be evaluatedInfinity;//Infinity is infinitely large, and is represented as infinity when the value exceeds the maximum value that JavaScript can represent.Because the computer uses binary, it is sometimes convenient to use hexadecimal notation for integers, and hexadecimal is represented by 0x prefixes and 0-9,a-f, for example: 0XFF00,0XA5B4C3D2, and so on, they are exactly the same as the decimal values. Number can do arithmetic directly, and the rules and math are consistent:1 + 2;//3(1 + 2) * 5/2;//7.52/0;//Infinity0/0;//NaN10% 3;//110.5% 3;//1.5Noteis to find the remainder operation. String strings are in single quotation marksAny text enclosed in ' or double quotes ', such as ' abc ', ' XYZ ', and so on. Note that the ' or ' itself is only a representation, not a part of the string, so the string ' abc 'only the 3 characters of A,b,c. Boolean Boolean values and Boolean algebra are fully consistent, a Boolean value of only true, false two values, either true, or false, can be directly with true, False to indicate a Boolean value, can also be calculated by Boolean operation: true; This is a true value of false; This is a false value of 2 > 1; This is a true value of 2 >= 3; This is a false value && operation is associated with an operation, and only all of the results of the true,&& operation are true:true && true; This && statement evaluates to Truetrue && false; This && statement evaluates to Falsefalse && true && false; This && statement evaluates to false| | An operation is or an operation, as long as there is one for true,| | The result of the operation is True:false | | False This | | Statement evaluates to Falsetrue | | False This | | Statement evaluates to TrueFalse | | true | | False This | | The statement evaluates to the true! operation is a non-operation, it is a single-mesh operator, turns true into False,false true:! True The result is false! False The result is true! (2 > 5); The result is true that Boolean values are often used in conditional judgments, such as: var age = 15;if (age >=) {alert (' Adult ');} else {alert (' Teenager ');} Comparison operators when we compare number, we can get a Boolean value by comparison operator: 2 > 5; FALSE5 >= 2; True7 = = 7; True, JavaScript allows comparison of arbitrary data types: false = = 0; TrueFalse = = 0; False to pay special attention to equality operator = =. JavaScript is designed with two comparison operators: the first is = = comparison, which automatically converts data types and compares them, often with very bizarre results, and the second is the = = = comparison, which does not automatically convert data types, if the data type is inconsistent, returns false, if consistent, and then compare. Because of the design flaws of JavaScript, do not use = = comparison, always adhere to the use = = = comparison. Another exception is that the special number of Nan is not equal to all other values, including its own: Nan = = = Nan; False the only way to judge NaN is through the IsNaN () function: IsNaN (NaN); True finally pay attention to the equal comparison of floating-point numbers: 1/3 = = = (1-2/3); False this is not a design flaw for JavaScript. Floating-point numbers generate errors during operation because the computer cannot accurately represent infinite loop decimals. To compare two floating-point numbers for equality, you can only calculate the absolute value of their difference to see if they are less than a certain threshold: Math.Abs (1/3-(1-2/3)) < 0.0000001; Truenull and Undefinednull represent an "empty" value, and 0 and an empty string"Different, 0 is a number, '"represents a string of length 0, and null means "null". In other languages, there are also JavaScript-like null representations, such as Java, which is also used with the Null,swift Nil,python with none. However, in JavaScript, there is also a null-like undefined, which represents "undefined." The designer of JavaScript wants NULL to represent an empty value, while undefined indicates that the value is undefined. It turns out that there is no such thing as an egg, and the difference between the two is insignificant. In most cases, we should all use NULL. Undefined is useful only when judging whether a function parameter is passed or not. An array of arrays is a set of sequentially arranged collections, each of which is called an element. An array of JavaScript can include any data type. For example: [1, 2, 3.14,' Hello ', NULL, true]; The above array consists of 6 elements. Arrays are denoted by [], separated by elements. Another way to create an array is through the array () function: New Array (1, 2, 3); Array created [1, 2, 3] However, for the sake of readability of the code, it is strongly recommended to use [] directly. The elements of an array can be accessed through an index. Note that the starting value for the index is 0:var arr = [1, 2, 3.14,' Hello ', NULL, true];arr[0]; Returns an element with an index of 0, which is 1arr[5]; Returns an element with an index of 5, which is truearr[6]; The index is out of range, and the object that returns the undefined object JavaScript is a set of unordered collections of key-value, for example: var person = {Name:' Bob ', AGE:20, Tags: [' JS ', ' web ', ' mobile '], City:' Beijing ', Hascar:true, zipcode:null}; The key of a JavaScript object is a string type, and the value can be any data type. The person object above defines a total of 6 key-value pairs, each of which is also called a property of the object, for example, the Name property' Bob ', the ZipCode property is null. To get the properties of an object, we use the object variable. How to Name the property: Person.name; // ' Bob 'Person.zipcode;//The concept of a NULL variable variable is basically the same as the equation variable of the middle school algebra, except that in a computer program, a variable can be not only a number, but also any data type. Variables in JavaScript are represented by a variable name, which is a combination of uppercase and lowercase English, numeric, $, and _, and cannot begin with a number. The variable name cannot be a JavaScript keyword, such as if, while, and so on. Declare a variable with VAR statement, for example: Var A; The variable A is declared, at this point the value of a is undefinedvar $b = 1; The variable $b is declared, and $b is assigned at the same time, the value of $b is 1var s_007 =' 007 ';//s_007 is a string var Answer = true;//Answer is a Boolean truevar t = null;//T's value is a NULL variable name can also be in Chinese, but please do not bother yourself. In JavaScript, use equal sign = To assign a value to a variable. Any data type can be assigned to a variable, the same variable can be repeatedly assigned value, and can be different types of variables, but note that only with the Var declaration, for example: var a = 123; The value of a is an integer 123a =' ABC ';//A becomes a string the variable itself is a language that is not fixed as a dynamic language, and corresponds to a static language. Static languages must specify the variable type when defining the variable, and if the type does not match, an error is given. For example, Java is a static language, the assignment statement is as follows: int a = 123; A is an integer type variable, and the type is declared with int a = "ABC"; Error: You cannot assign a string to an integer variable compared to a static language, which is why dynamic languages are more flexible. Do not equate an equal sign of an assignment statement with a mathematical equal sign. For example, the following code: var x = 10;x = x + 2; If mathematically understood x = x + 2 That is not true anyway, in the program, the assignment statement first calculates the right expression X + 2, obtains the result 12, and assigns to the variable x. Since the value before X is 10, the value of X becomes 12 after the value is re-assigned. Strict mode JavaScript at the beginning of the design, in order to facilitate learning for beginners, does not require the use of VAR declaration variables. This design error has serious consequences: if a variable is not used by the Var declaration, then the variable is automatically declared as a global variable: i = 10; I am now a global variable in a different JavaScript file on the same page, and if you do not use the Var declaration, the variable i is used, which will cause the variable I to interact with each other and produce error results that are difficult to debug. A variable declared with VAR is not a global variable, its scope is limited to the body of the declared function of the variable (the concept of the function will be explained later), and the variable of the same name does not conflict with each other in the function body. In order to fix the serious design flaw of JavaScript, ECMA introduced the strict mode in the following specification, the JavaScript code running in strict mode, forcing the Var declaration variable to be used without VAR declaration variable, will result in a run error. The way to enable strict mode is to write on the first line of the JavaScript code:' Use strict 'This is a string, a browser that does not support strict mode will execute it as a string statement, and a browser that supports strict mode will open strict mode to run JavaScript. To test if your browser can support strict mode:' Use strict '///If the browser supports strict mode,//The following code will report referenceerror error: ABC =' Hello, World '; alert (ABC); Run the code, if the browser error, please repair and then run. If the browser does not error, it means that your browser is too old and need to upgrade as soon as possible. Variables declared without var are considered global variables, and in order to avoid this flaw, all JavaScript code should use the strict pattern. The JavaScript code we write later will all be in strict mode. 

Javascriptav data types 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.