Explanation of JavaScript variables and data types-javascript tips-js tutorial

Source: Internet
Author: User
This article describes the JavaScript variables and Data Types in detail. For more information, see. 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.
I. Variables
JavaScript variables are loose, which is used to save any type of data. A variable is a container that stores information. When defining a variable, 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 ). Using 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 ). Variables can use short names (such as x and y), or better descriptive names (such as age, sum, 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.
Instance:

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.

   
  JS variables and Data Types     

Click here to create a variable and display the result.

Click here

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.