Easy to learn about JavaScript 5: JavaScript variables and Data Types

Source: Internet
Author: User
Tags javascript array

Easy to learn about JavaScript 5: JavaScript variables and Data Types

For a programming language, it must contain variables and data types. Today, let's take a look at the variables and Data Types of the JavaScript scripting language. Relative

In other advanced programming languages such as Java and C ++, JavaScript is very simple.

One Variable

JavaScript variables are loose, which is used to save any type of data. A variable is a container that stores information. When defining variables

Use the var operator (var is the keyword) followed by a variable name (the variable name is the identifier ). The variable is the amount that can be changed again after initialization.

Let's take a look at the instance:
var x=2;var y=3;var z=2+3;document.write(x + "");document.write(y + "");document.write(z + "");

Just like algebra: x = 2, y = 3, z = x + y in algebra, we use letters (such as x) to save values (such as 2 ). Use the above expression z = x + y,

We can calculate the value of z as 5. In JavaScript, these letters are called variables. Therefore, we can regard variables as containers that store data.

(1) JavaScript variable name

Like algebra, JavaScript variables can be used to store values (such as x = 2) and expressions (such as z = x + y ). You can use short names (such as x and y) for variables, or

You can use better descriptive names (such as age, sum, and totalvolume ).

Note that:

1. The variable must start with a letter.

2 variables can also start with $ and _ (but we do not recommend this)

3. Variable names are case sensitive (y and Y are different variables)

(2) JavaScript Data Type

JavaScript variables can also save other data types, such as text values (name = "Bill Gates "). In JavaScript, similar to "Bill Gates"

A text is called a string. JavaScript variables have many types, but now we only focus on numbers and strings. Assign text values to variables

The value should be enclosed in double quotation marks or single quotation marks. Do not use quotation marks when the value assigned to the variable is a value. If you enclose a value in quotation marks, the value is

As text. A detailed description of data types is provided later.

var pi=3.14;var name="Bill Gates";var answer='Yes I am!';document.write(pi + "");document.write(name + "");document.write(answer + "");

(3) declare (create) JavaScript Variables

Creating a variable in JavaScript is usually called a "Declaration" variable. A good programming habit is that at the beginning of the code, we should unify the sound of the required variables.

Ming. We recommend that you do not use var when declaring variables.

We use the var keyword to declare the variable: var carname;

After the variable is declared, the variable is empty (it has no value ). To assign a value to a variable, use the equal sign: carname = "Volvo ";

However, you can assign a value to a variable when declaring the variable: var carname = "Volvo ";

Example: we created a variable named carname, assigned "Volvo" to it, and put it in the HTML section of id = "demo.


Click here to create a variable and display the result.

Click here


<Script type = "text/javascript"> function myFunction () {var carname = "Volvo"; document. getElementById ("demo"). innerHTML = carname ;}</script>

Click effect:

(4) One Statement and multiple variables

You can declare many variables in a statement. This statement starts with var and uses commas to separate variables:


var name="Gates", age=56, job="CEO";



The statement can also span multiple rows:
var name="Gates",age=56,job="CEO";

Variables without values are often declared in computer programs. A variable declared without a value. Its value is actually undefined. The following statements have been executed:

The value of the variable carname will be undefined: var carname;

(6) redeclare JavaScript Variables

If you re-declare a JavaScript variable, the value of the variable will not be lost: After the following two statements are executed, the carname value of the variable is still "Volvo ":


var carname="Volvo";var carname;


(7) JavaScript Arithmetic

You can use JavaScript variables to calculate, using the plus + operator:




Assume y = 5, calculate x = y + 2, and display the result.

Click here


<Script type = "text/javascript"> function myFunction () {var y = 5; var x = y + 2; var demoP = document. getElementById ("demo") demoP. innerHTML = "x =" + x ;}</script>


Click effect:

2. Data Types

JavaScript data types include string, number, Boolean, array, object, Null, and Undefined. Let's talk about data types first.

Type of operator.

Typeof Operator

The typeof operator is used to detect the Data Type of a variable. If the typeof operator is used for values or variables, the following string is returned:

var box='English';alert(typeof box);alert(typeof English);

Both methods are feasible.

The typeof operator can operate on variables or literal values. Although typeof (box) can be used in this way, typeof is an operator rather than a built-in

Function. Functions are objects rather than data types. Therefore, it is necessary to use typeof to distinguish functions from objects.

An example of a function is returned:
Function box () {} alert (typeof box); // box is a Function with a value of function box () {}, and the type returns a function string.

(1) JavaScript has dynamic types

JavaScript has dynamic types. This means that the same variables can be of different types:

Var x // x is undefinedvar x = 6; // x is the number var x = "Bill"; // x is the string

(2) JavaScript String type

A string is a variable that stores characters. A string can be any text in quotation marks. You can use single or double quotation marks :;

Example: You can use quotation marks in a string as long as they do not match the quotation marks surrounding the string.
var carname1="Bill Gates";var carname2='Bill Gates';var answer1="Nice to meet you!";var answer2="He is called 'Bill'";var answer3='He is called "Bill"';document.write(carname1 + "")document.write(carname2 + "")document.write(answer1 + "")document.write(answer2 + "")document.write(answer3 + "")

The string type also defines escape characters:

(3) JavaScript numbers

JavaScript has only one numeric type. A number can contain a decimal point or not. The Number type contains two types of values: integer and floating point. Input

The output format is output in decimal format. The most basic numeric literal is decimal. It also includes the octal value literal. The leading value must be 0 and the octal order.

Column (0 to 7, based on 8); the first two digits of the hexadecimal literal must be 0x, followed by (0 to 9 and A to F); floating point type, is required in the value.

Contains a decimal point, and must have at least one digit after the decimal point.

1 For those values that are too large or too small, we can use scientific notation (e Notation) to use e to represent the first 10 exponent power of the value. For example:


var box=4.12e-9;


2. If you want to determine whether a value exceeds the specified range, you can use the isFinite () function. If the value does not exceed, true is returned. If the value exceeds the specified range, true is returned.


3 The isNaN () function is used to determine whether the value is NaN. After the isNaN () function receives a value, it tries to convert the value to a value.

The isNaN () function also applies to objects. When the isNaN () function is called, the value () method is called first, and then whether the returned value can be converted

Value. If not, call the toString () method based on the returned value, and then test the returned value.

var x1=36.00;var x2=36;var y=123e5;var z=123e-5;document.write(x1 + "")document.write(x2 + "")document.write(y + "")document.write(z + "")

(4) JavaScript Boolean

Boolean (logical) can have only two values: true or false. For example:


var x=true;var y=false;


(5) JavaScript Array

The array subscript is zero-based, so the first project is [0], the second project is [1], and so on. The following code creates an array named cars:
var cars=new Array();cars[0]="Audi";cars[1]="BMW";cars[2]="Volvo";
var cars=new Array("Audi","BMW","Volvo"); 
var i;var cars = new Array();cars[0] = "Audi";cars[1] = "BMW";cars[2] = "Volvo";for (i=0;i

The output result is easy to know.

(6) JavaScript objects

Objects are separated by curly brackets. Within the brackets, the attributes of an object are defined in the form of name and value pairs (name: value. Attributes are separated by commas:


var person={firstname:"Bill", lastname:"Gates", id:5566};


The object (person) in the preceding example has three attributes: firstname, lastname, and id. Spaces and lines do not matter. The statement can span multiple rows:
var person={firstname : "Bill",lastname  : "Gates",id:  5566};

There are two addressing methods for object attributes:

var person={firstname : "Bill",lastname : "Gates",id:  5566};document.write(person.lastname + "");document.write(person["lastname"] + "");

(7) Undefined and Null

Undefined indicates that the variable does not contain any value. You can clear a variable by setting its value to null.

Undefined type


Var box; alert (typeof box); // The box is of the Undefined type, the value is undefined, and the string returned by the type is undefined.


Null type


Var box = null; alert (typeof box); // The box is of the Null type, the value is null, and the returned string is an object.


(8) Declare the variable type

JavaScript variables are all objects. When you declare a variable, a new object is created. When declaring a new variable, you can use the key

To declare its type:


var carname=new String;var x=      new Number;var y=      new Boolean;var cars=   new Array;var person= new Object;



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.