Variables and data types in JavaScript

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/mygis2005/article/details/7375419

JavaScript is a weakly typed language, and variable names, operators, and method names are case-sensitive.

1. Variable definition

In JavaScript, you use var to define any type of variable, and each variable is just a placeholder for holding the data.

[JavaScript]View Plaincopy
    1. var temp;  //This code defines a variable, but its type is unknown, it can hold any type of value, and when it is not initialized, the store in test is undefined.
    2. var temp=2;  //This code defines a variable and is directly initialized to a numeric type.
    3. var temp="JavaScript";  //This code defines a variable and directly initializes the micro string type, both single and double quotes, as long as the pair appears in the line.

2. Scope of variables

In JavaScript, variables defined with VAR are scoped to the method or function that defines the variable. In other words, variables defined with VAR are local variables.

Cases:

[JavaScript]View Plaincopy
    1. function test ()
    2. {
    3.  var temp="Hello, javascript!";
    4. }
    5. Test ();  //Method call creates a variable and initializes it, and the variable is destroyed after execution is completed.
    6. Alert (temp); //undefined.  Because the variable temp has been destroyed, it is undefined (undefined).

If Var is not used when defining a variable, then the defined variable is a global variable.

Cases:

[JavaScript]View Plaincopy
    1. function Test2 ()
    2. {
    3. temp2="Hello, javascript!";
    4. }
    5. Test2 ();  when the method is called, the variable is created and initialized, and the variable remains after the execution has ended.
    6. alert (TEMP2);  //hello, javascript! Variable value still exists

3. Data type

Although JavaScript is a weakly typed language, it also has several data types: number, String, Boolean, Object, udefined, and Null. Where object is a complex data type, object consists of unordered key-value pairs. The remaining categories are simple data types. Note: The first letter of the variable type is capitalized, and the first letter of the variable value is lowercase.

JavaScript does not support custom types, so all values in JavaScript belong to one of these six types.

To figure out what type of data a variable is, use the typeof operator, and note, in particular, that TypeOf is an operator, not a method, so the letter ' O ' in typeof is lowercase.

Syntax: typeof temp; Temp is a variable, it can be without parentheses, but it's best to add parentheses for readability.

the difference between JavaScript [] and {}

[] is an array form, {} is an object form and can contain other types.
such as Var a= ["A", "B", {a:1,b:2}];
A[1] obtained is that the b,a[2].b obtained is 2;
var s = {a:1,b:["a", "B"]}
S.A. obtained is 1,S.B[1] obtained is a

Variables and data types in JavaScript

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.