JavaScript variables and data types

Source: Internet
Author: User
Tags binary to decimal first string pow

1.JavaScript variables

JavaScript variables are loosely typed (weakly typed), meaning that a variable can be used to hold any type of data, and each variable is simply a placeholder for the value to hold. The declaration of a variable is defined with the var operator, such as Var message, or it can be defined together with multiple variables, such as Var a,b,c; separate each variable with a comma.

VAR message;

var message; // The message is initialized with a value of undefinedmessage=12; // The message value is:message= "Hello"; // The message value is hello; message=["Hello", 12]; // message value is an array

The above code explains that the JavaScript variable is loosely typed and can have different types of values. If the variable is defined without the keyword VAR, then the variable is a global variable, it is not recommended to use too many global variables, is not easy to manage, resulting in code confusion, and too many global variables will consume a lot of memory, because the global variables until the page is destroyed, the global variable is destroyed.

a=12; // at this point A is a global variable

2.JavaScript Data types

Undefined type

The undefined type is only a special value of undefined. A variable is declared, but is not assigned a value of undefined. Such as

var name;console.log (name==undefined); // Output True

The above definition is equivalent to the Var name=undefined,undefined is a special value. It is necessary to note that the undefined variable is different from the undefined variable. Undefined variables cannot be used directly, resulting in an exception.

var name;console.log (name==undefined); // Output Trueconsole.log (sex); // error, sex is not defined

Of course, when you use typeof to get the value type of a variable, the value of the undefined variable and the value of the already defined (uninitialized) variable are "undefined".

var name;console.log (name==undefined); // Output Trueconsole.log (typeof Sex); // output ' undefined 'console.log (typeof name); // output ' undefined '

Null data type

The null data type is also a data type with only one value, and this value is null. From a logical point of view, NULL represents a null pointer. typeof Null will return "object";

var age=null; Console.log (typeof age); // output ' undefined '

Boolean type

The Boolean type uses a very great number of data types in JavaScript, which contains two values of true and false. True is not necessarily a numeric value 1,false is not necessarily a numeric value of 0.

Data type

Convert to True

converted to false

Boolean

True

false

string

Non-empty string

" "

number

Any non-0 numeric value (including infinity)

0 and nan

Object

Any object

Null

Undefined

N/a

undefined

Nunber type

The number type is used to represent integers and floating-point numbers. In JavaScript, there is no difference between an integer value and a floating-point value, and a JavaScript number can be any of two types.

The number data type can be used to represent decimal numbers, such as Var num=1; var num1=1.2; In addition to decimal, number can also represent octal and hexadecimal integers. Octal values the first bit is expressed as a value of 0-7, followed by the literal value of the expression, if the literal value is out of range, then 0 is ignored, when the decimal to parse.

var num1 = 023; var num2 = 079var num3=08; Console.log (NUM1); // 19 valid octal value, outputconsole.log (num2); // invalid octal value, outputconsole.log (num3); // invalid octal value, output 8

Hexadecimal literals The preceding two bits are 0x, followed by any hexadecimal digits (0-9, a-f), and the letters can be lowercase to denote 10-15;

var liu1 = 0xaf; var liu2 = 0x23; console.log (LIU1); // 175Console.log (LIU2); //  *

hexadecimal and octal when calculating, JavaScript converts octal and 16 binary to decimal, and then calculates.

Octal Decimal Method: First remove the previous 0, and then by the weight of the addition method, the number of octets on each bit multiplied by the right, and then the number of added together. such as 023=2*math.pow (8,1) +3*math.pow (8,0) = 19;

Hexadecimal to Decimal method: First remove the 0x, and then convert the letter to a number of weights to add the method. such as 0xaf=10*math.pow (16,1) +15*math.pow (16,0) = 175;

The minimum value that JavaScript can represent is saved in Number.min_value, with a value of 5e-324 and the maximum value in Number.MAX_VALUE, with a value of 1.7969e+308. If the value is out of range, it is converted to infinity.

Nan, which represents non-data, Nan is not equal to any value, and the IsNaN () function is used to determine whether the parameter is a non-numeric value.

Console.log (IsNaN (NaN)); // trueconsole.log (IsNaN (10)); // falseConsole.log (IsNaN (0)); // falseConsole.log (IsNaN ("8a")); // trueconsole.log (IsNaN ("67")); // false

numeric conversions,

parseint (), parsefloat ();

Paseint is used to convert strings to numbers. Decimal, octal, hexadecimal can be converted. Parses from the first string until it resolves to a character other than a number. Nan If the first is a literal value that is not a number

Console.log (parseint ("023", 8)); // outputConsole.log (parseint ("83a2")); // outputConsole.log (parseint ("a83a2")); // Output Nan

JavaScript variables 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.