A Brief Introduction to JavaScript variables and data types, and a brief introduction to javascript

Source: Internet
Author: User

A Brief Introduction to JavaScript variables and data types, and a brief introduction to javascript

JavaScript data type:

One of the most fundamental features of a programming language is a set of data types it supports. These are the types of values that can be expressed and manipulated in programming languages.

JavaScript allows three basic data types:

  1. Numbers, such as. 123,120.50.
  2. Such as "This text string.
  3. Boolean type, such as true or false.

JavaScript also defines two data types: null and undefined, each of which limits only one single value.

In addition to these basic data types, JavaScript supports Composite data types called objects. We will see the object details in a separate chapter.

Note: Java does not differentiate between integers and floating-point values. All numbers in JavaScript are expressed as floating point values. JavaScript represents a number that uses the 64-bit floating point format defined in the IEEE 754 standard.
JavaScript variable:

Like many other programming languages, JavaScript has variables. Variables can be considered named containers. You can put the data in these containers, and then refer to the data to simply name the container.

To use a variable in a JavaScript program, you must declare it. Variables are declared with the var keyword as follows:

<script type="text/javascript"><!--var money;var name;//--></script>

You can also declare multiple variables with the same var keyword as follows:

<script type="text/javascript"><!--var money, name;//--></script>

The value stored in the variable is called the initialization of the variable. You can initialize a variable when it is created or updated. A variable is required, as shown below:

For example, you can create a variable named "money" and "2000.50" and assign it to it. For another variable, you can assign a value during initialization as follows:

<script type="text/javascript"><!--var name = "Ali";var money;money = 2000.50;//--></script>

Note: Use the var keyword to declare or initialize only. Once the variable name declares its lifecycle throughout the document. You do not need to declare the same variable twice.

JavaScript is a typed language. This means that JavaScript variables can save values of any data type. Unlike many other languages, variables that do not have to declare any type of value in variables will hold to tell JavaScript. The Value Type of the variable can be changed during the execution of the program, and JavaScript will automatically.
JavaScript variable range:

The range of a variable is the region defined in the program. JavaScript variables have only two categories.

  • Global variables: global variables have a global scope, which means they are everywhere in JavaScript code.
  • Local variable: The local variable will only be visible to the defined function. Function parameters are partial functions.

In a function, local variables take precedence over global variables with the same name. If the Declaration has the same name as a global variable, a local variable or function parameter, the global variable can be effectively hidden. The following is an example:

<script type="text/javascript"><!--var myVar = "global"; // Declare a global variablefunction checkscope( ) {  var myVar = "local"; // Declare a local variable  document.write(myVar);}//--></script>

This produces the following results:

local

 
JavaScript variable name:

Although the variable naming rules in JavaScript are as follows.

  • No reserved JavaScript keywords should be used as variable names. The next part of these keywords. For example, the break or boolean variable name is invalid.
  • JavaScript variable names should not start with numbers (0-9 ). They must contain letters or underscores. For example, 123test is invalid variable name, But _ 123tes t is valid.
  • JavaScript variable names are case sensitive. For example, Name and name are two different variables.

Reserved JavaScript words:

Below are reserved words in JavaScript. They cannot be used for JavaScript variables, functions, methods, cyclic tags, or any object name.

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.