JavaScript Learning notes (3)

Source: Internet
Author: User

Data types for JavaScript

Character type, number, Boolean, array, object, Null, Undefined

Definition of the array:

var cars=new Array ();

Cars[0]= "Audi";

Cras[1]= "BMW";

Cars[3]= "Benze";

Or:

var cars = new Array ("Audi", "BMW", "Benze");

Or:

var cars =["Audi", "Audi", "Audi", "Audi", "Audi";

Such as:

<! DOCTYPE html>

<body>

<script>
var i;
Mode 1
var cars1 = new Array ();
Cars1[0] = "Audi";
CARS1[1] = "BMW";
CARS1[2] = "Volvo";

Mode 2:
var cars2 = new Array ("Audi", "BMW", "Volvo", "Audi", "BMW", "Volvo");

Mode 3
var cars3=["Audi", "Audi", "Audi", "Audi", "Audi";

for (i=0;i<cars3.length;i++)
{
document.write (Cars3[i] + "<br>");
}
</script>

</body>

JavaScript objects

Objects are separated by curly braces. Inside the parentheses, the properties of the 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 example above has three attributes: FirstName, LastName, and ID.
Spaces and lines do not matter. Declarations can span multiple lines:
var person={
FirstName: "Bill,"
LastName: "Gates",
id:5566
};
Object properties are addressed in two ways:

Name=person.lastname;

name=person["LastName"];

Such as:

<! DOCTYPE html>

<body>

<script>
var person={
FirstName: "Bill,"
LastName: "Gates",
id:5566
};


document.write (person.firstname+ "+person.lastname" + "+person.id+" <br/> ");
document.write (person["FirstName"]+ "+person[" LastName "]+" to "+person[" "id"] + "<br/>");
</script>

</body>

Undefined and Null

Undefined this value means that the variable does not contain a value

Null can be used to empty variables

Such as:

<! DOCTYPE html>

<body>

<script>
var person;
var car= "Volvo";
document.write (person + "<br/>");
document.write (car + "<br/>" + "<br/>");

var car=null
var person= "Steve Jobs";
document.write (person + "<br/>");
document.write (car + "<br/>");
</script>

</body>

Output Result:

Undefined
Volvo

Steve Jobs
Null

Declaring variable types

You can declare the type of a variable by using the New keyword

Such as:

<! DOCTYPE html>

<body>

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

<p id= "Test1" ></p>
<p id= "Test2" ></p>
<p id= "Test3" ></p>
<p id= "Test4" ></p>

<script>
function MyFunction ()
{
var numval = new number;
Numval = 123.123;

var strval = new String;
Strval = "This is A STRING.";

var bVal = new Boolean;
BVal = true;

var arrval = new Array;
arrval[0]=100;
arrval[2]=200;
arrval[3]=300;
arrval[4]=400;
arrval[5]=500;

document.getElementById ("Test1"). Innerhtml=numval;
document.getElementById ("Test2"). Innerhtml=strval;
document.getElementById ("Test3"). Innerhtml=bval;
document.getElementById ("Test4"). Innerhtml=arrval[3];
}
</script>

</body>

Output Result:

<! DOCTYPE html>

<body>

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

<p id= "Test1" ></p>
<p id= "Test2" ></p>
<p id= "Test3" ></p>
<p id= "Test4" ></p>

<script>
function MyFunction ()
{
var numval = new number;
Numval = 123.123;

var strval = new String;
Strval = "This is A STRING.";

var bVal = new Boolean;
BVal = true;

var arrval = new Array;
arrval[0]=100;
arrval[2]=200;
arrval[3]=300;
arrval[4]=400;
arrval[5]=500;

document.getElementById ("Test1"). Innerhtml=numval;
document.getElementById ("Test2"). Innerhtml=strval;
document.getElementById ("Test3"). Innerhtml=bval;
document.getElementById ("Test4"). Innerhtml=arrval[3];
}
</script>

</body>

Click the button to output the result:

123.123

This is A STRING.

True

300

JavaScript Learning notes (3)

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.