Easy to learn JavaScript five: JavaScript variables and data types

Source: Internet
Author: User
Tags javascript array



For a programming language, it must contain variables and data types. Today we'll look at the variables and data types of the JavaScript scripting language. Relative



In other high-level programming languages such as java,c++, JavaScript seems simple.



A variable



JavaScript variables are loosely typed, and so-called loose is used to hold any type of data. A variable is a container for storing information. When defining variables



To use the var operator (Var is the keyword), followed by a variable name (the variable name is an identifier). Variables are the amount that can be changed again after initialization.


so let's take a look at the example:

<span style = "font-size: 18px;"> var x = 2;
var y = 3;
var z = 2 + 3;
document.write (x + "<br>");
document.write (y + "<br>");
document.write (z + "<br>"); </ span>
       Just like algebra: x = 2, y = 3, z = x + y In algebra, we use letters (such as x) to save values (such as 2). Through the above expression z = x + y,

We can calculate the value of z as 5. In JavaScript, these letters are called variables. So we can think of variables as containers for storing 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). Variables can use short names (such as x and y), also

You can use more descriptive names (such as age, sum, total volume).

       have to be aware of is:

       1 Variable must start with a letter

       2 Variables can also start with $ and _ symbols (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 hold other data types, such as text values (name = "Bill Gates"). In JavaScript, something like "Bill Gates"

A piece of text is called a character string. There are many types of JavaScript variables, but for now, we only focus on numbers and strings. Assign text values to variables

, You should surround the value with double or single quotes. When the value assigned to a variable is a numeric value, do not use quotation marks. If you enclose the value in quotation marks, the value will be

Treat as text. There is a detailed data type introduction later.

       Examples:
<span style = "font-size: 18px;"> var pi = 3.14;
var name = "Bill Gates";
var answer = 'Yes I am!';
document.write (pi + "<br>");
document.write (name + "<br>");
document.write (answer + "<br>"); </ span>
       (3) Declare (create) JavaScript variables

       Creating variables in JavaScript is often referred to as "declaring" variables. A good programming habit is that at the beginning of the code, the required variables should be unified

Bright. It is also possible not to use var when declaring variables, but this is not recommended.

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

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

      However, you can also assign values to variables when they are declared: var carname = "Volvo";

      Example: We created a variable named carname and assigned "Volvo" to it, then put it in the HTML paragraph with id = "demo".

<span style = "font-size: 18px;"> <! DOCTYPE html PUBLIC "-// W3C // DTD XHTML 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/ xhtml1-transitional.dtd ">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv = "Content-Type" content = "text / html; charset = gb2312" />
<title> JS variables and data types </ title>
</ head>

<body>
<p> Click here to create variables and display the results. </ p>

<button onclick = "myFunction ()"> Click here </ button>

<p id = "demo"> </ p>

<script type = "text / javascript">
function myFunction ()
{
var carname = "Volvo";
document.getElementById ("demo"). innerHTML = carname;
}
</ script>
</ body>
</ html> </ span>
      Click effect:



      (4) One statement, multiple variables

      You can declare many variables in one statement. The statement starts with var and separates the variables with commas:


var name = "Gates", age = 56, job = "CEO";
      The statement can also span multiple lines:
<span style = "font-size: 18px;"> var name = "Gates",
age = 56,
job = "CEO"; </ span>
      In computer programs, variables with no value are often declared. For variables declared without values, the value is actually undefined. After executing the following statement

After that, the value of the variable carname will be undefined: var carname;

      (6) Redeclare JavaScript variables

      If you redeclare the JavaScript variable, the value of the variable will not be lost: after the following two statements are executed, the value of the variable carname is still "Volvo"

<span style = "font-size: 18px;"> var carname = "Volvo";
var carname; </ span>
      (7) JavaScript arithmetic

      You can do arithmetic with JavaScript variables, using operators such as and +:
      example:

<span style = "font-size: 18px;"> <! DOCTYPE html PUBLIC "-// W3C // DTD XHTML 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/ xhtml1-transitional.dtd ">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv = "Content-Type" content = "text / html; charset = gb2312" />
<title> JS variables and data types </ title>
</ head>

<body>
<p> Assuming y = 5, calculate x = y + 2, and display the result. </ p>
<button onclick = "myFunction ()"> Click here </ button>

<p id = "demo"> </ p>

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



       Two data types
       The data types of JavaScript include string, number, boolean, array, object, Null, Undefined. Before we talk about data types, let ’s talk about one

Operator typeof.

       typeof operator

       The typeof operator is used to detect the data type of a variable. Using the typeof operator for values or variables will return the following string:

      

<span style = "font-size: 18px;"> var box = 'English';
alert (typeof box);
alert (typeof English); </ span>
       Both of the above methods are feasible.

       The typeof operator can operate on variables or literals. Although it can be used in this way, typeof (box), but typeof is an operator rather than built

function. Functions are objects, not a data type, so it is necessary to use typeof to distinguish between function and object.

       The return value is an example of a function:
<span style = "font-size: 18px;"> function box () {
}
alert (typeof box); // box is the Function function, the value is function box () {}, and the type string returned is function </ span>
       (1) JavaScript has dynamic types

       JavaScript has dynamic types. This means that the same variable can be used for different types:

       Examples:
<span style = "font-size: 18px;"> var x // x is undefined
var x = 6; // x is a number
var x = "Bill"; // x is a string </ span>
       (2) JavaScript string String type

       Strings are variables that store characters. The string can be any text in quotation marks. You can use single or double quotes :;

       Example: You can use quotation marks in the string, as long as it does not match the quotation marks surrounding the string
<span style = "font-size: 18px;"> 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 + "<br>")
document.write (carname2 + "<br>")
document.write (answer1 + "<br>")
document.write (answer2 + "<br>")
document.write (answer3 + "<br>") </ span>
       The string type also defines escape characters:



        (3) JavaScript numbers

       JavaScript has only one numeric type. Numbers can have a decimal point or not. The Number type contains two types of values: integer and floating point. lose

The output format is output in decimal numbers. The most basic numeric literal is decimal. Also includes octal numeric literals, the leading must be 0, octal order

Column (0 to 7, with 8 as the base); the first two digits of the hexadecimal literal must be 0x, followed by (0 to 9 and A to F); the floating-point type is that the value must be included

Contains a decimal point, and there must be 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), with e representing the exponential power of the value in front of 10. E.g:

<span style = "font-size: 18px;"> <span style = "font-size: 18px;"> var box = 4.12e-9; </ span> </ span>
       2 To determine whether a value exceeds the specified range in the end, you can use the isFinite () function, if it does not exceed, return true, return if exceeded

false.

       The 3isNaN () function is used to determine whether this value is NaN. The isNaN () function will try to convert this value to a numeric value after receiving a value.

The isNaN () function also applies to objects. In the process of calling the isNaN () function, the value () method is called first, and then it is determined whether the return value can be converted to a number

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

       Examples:
<span style = "font-size: 18px;"> var x1 = 36.00;
var x2 = 36;
var y = 123e5;
var z = 123e-5;
document.write (x1 + "<br />")
document.write (x2 + "<br />")
document.write (y + "<br />")
document.write (z + "<br />") </ span>
       (4) JavaScript boolean

       Boolean (logical) can only have two values: true or false. E.g:


var x = true;
var y = false;
       (5) JavaScript array

       The array index is based on zero, so the first item is [0], the second is [1], and so on. The following code creates an array named cars:
<span style = "font-size: 18px;"> var cars = new Array ();
cars [0] = "Audi";
cars [1] = "BMW";
cars [2] = "Volvo"; </ span>
       or:
<span style = "font-size: 18px;"> var cars = new Array ("Audi", "BMW", "Volvo"); </ span>
       Examples
<span style = "font-size: 18px;"> var i;
var cars = new Array ();
cars [0] = "Audi";
cars [1] = "BMW";
cars [2] = "Volvo";
for (i = 0; i <cars.length; i ++)
{
document.write (cars [i] + "<br>");
} </ span>
       The output is easy to know.

       (6) JavaScript object

       The objects are separated by curly braces. Inside the parentheses, the attributes of the object are defined in the form of name and value pairs (name: value). The attributes are separated by commas:


var person = {firstname: "Bill", lastname: "Gates", id: 5566};
       The person in the above example has three attributes: firstname, lastname, and id. Spaces and line breaks are irrelevant. The declaration can span multiple lines:
var person = {
firstname: "Bill",
lastname: "Gates",
id: 5566
};
       There are two addressing modes for object attributes:

       Examples
var person = {
firstname: "Bill",
lastname: "Gates",
id: 5566
};
document.write (person.lastname + "<br />");
document.write (person ["lastname"] + "<br />");
       (7) Undefined and Null

       The value Undefined indicates that the variable contains no value. You can clear the variable by setting the value of the variable to null.

       Undefined type


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


var box = null;
alert (typeof box); // The box is null type, the value is null, and the string returned by the type is object.
        (8) Declare variable type

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

The word "new" to declare its type:


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





Easy to learn JavaScript 5: 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.