Variable declarations and data types in JavaScript

Source: Internet
Author: User

    1. JavaScript Variables

variable names must begin with a letter or underscore ("_")

variables can also start with the $ and _ symbols (although we do not recommend this)

variable names are case sensitive (y and y are different variables)

variables can contain numbers, from A to Z uppercase and lowercase letters

Tips: Both JavaScript statements and JavaScript variables are case-sensitive .

Var A;

"var"-the keyword used to declare a variable

"a"-Variable name

Declaring and initializing variables at the same time

var a= 10;

Declaring multiple variables

var x, y, z = ten; ( only z has This value , the rest is undefined)

    1. JavaScript Data Types

JavaScript has a dynamic type

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

var x//x is undefined

var x = 6; X is a number

var x = "Bill"; X is a string

(1) JavaScript string

strings are stored characters (such as The variable of "Bill Gates").

The string can be any text in quotation marks. You can use single or double quotation marks:

var carname= "Bill Gates";

var carname= ' Bill Gates ';

(2) JavaScript numeric type

JavaScript has only one numeric type. Numbers can be with decimal points or without:

var x1=34.00; use a decimal point to write

var x2=34; do not use the decimal point to write

Large or small numbers can be written by means of scientific (exponential) notation:

var y=123e5; 12300000

var z=123e-5; 0.00123

JavaScript Boolean

(3) a Boolean (logic) can have only two values:true or false.

    1. Undefined and Null

Undefined This value indicates that the variable does not contain a value.

you can empty a variable by setting the value of the variable to null .

For example:

<script>

var person;

var car= "Volvo";

document.write (person + "<br/>");

document.write (car + "<br/>");

var car=null

document.write (car + "<br/>");

</script>

    1. Declaring variable types

when declaring a new variable, you can use the keyword "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;

JavaScript variables are objects. When you declare a variable, a new object is created.

Note: all things in JavaScript are objects: strings, numbers, arrays, dates, and so on.

You can also create your own objects.
This example creates a name named "Person" and adds four properties to it:
<script>
Person=new Object ();
Person.firstname= "Bill";
Person.lastname= "Gates";
person.age=56;
Person.eyecolor= "Blue";
document.write (Person.firstname + "is" + Person.age + "Years");
</script>

Variable declarations and data types in JavaScript

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.