JavaScript data...LOGIN

JavaScript data types

JavaScript data types have the following types

  • String

  • Number

  • Boolean

  • Array

  • Object(Object)

  • Null

  • Undefined.


JavaScript has dynamic typing

JavaScript has dynamic typing. This means that the same variable can be used as different types, like this

var x; var x = "John"; // Now x is a string


JavaScript string
Strings are variables that store characters (such as "Bill Gates").

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

var carname="Volvo XC60";

var carname='Volvo XC60';

You can use quotes within a string as long as it does not match the quotes surrounding the string:

Example

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>PHP中文网(PHP.CN)</title>
</head>
<body>
<script>
    var carname1="我是双引号";
    var carname2='我是单引号';
    var answer1="这是正常的";
    var answer2="我的名字是 'Johnny'";
    var answer3='我的名字是 "Johnny"';
    document.write(carname1 + "<br>")
    document.write(carname2 + "<br>")
    document.write(answer1 + "<br>")
    document.write(answer2 + "<br>")
    document.write(answer3 + "<br>")
</script>
</body>
</html>
Run the code Try it


JavaScript NumbersJavaScript has only one number type. Numbers can be with or without a decimal point:

var x1=34.00; //Use decimal point to write
var x2=34; // //Do not use decimal point to write

very large or very small numbers It can be written in scientific (exponential) notation:

Example

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>PHP中文网(php.cn)</title> 
</head>
<body>
<script>
var x1=34.00;
var x2=34;
var y=123e5;
var z=123e-5;
document.write(x1 + "<br>")
document.write(x2 + "<br>")
document.write(y + "<br>")
document.write(z + "<br>")
</script>
</body>
</html>

Run the program to try it


JavaScript Boolean

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

var x=true;
var y=false;


JavaScript Array

The following code creates an array named cars:

var cars=new Array();
cars[0]="Saab";
cars [1]="Volvo";
cars[2]="BMW";

or (condensed array):

var cars=new Array( "Saab","Volvo","BMW");

or (literal array):

Example

<!DOCTYPE html>
<html>
<body>
<script>
    var i;
    var cars = new Array();
    cars[0] = "Saab";
    cars[1] = "Volvo";
    cars[2] = "BMW";
    for (i=0;i<cars.length;i++)
    {
        document.write(cars[i] + "<br>");
    }
</script>
</body>
</html>

Run the program and try it

Tips: Array subscripts are zero-based, so the first item is [0] and the second is [1], so analogy.


JavaScript Objects

Objects are separated by curly braces. Inside the brackets, the object's properties are defined as name and value pairs (name : value). Properties are separated by commas:

var person={firstname:"John", lastname:"Doe", id:5566};

Object in the above example ( person) has three attributes: firstname, lastname and id.

Spaces and line breaks don't matter. The statement can span multiple lines:

var person={
firstname : "John",
lastname : "Doe",
id : 5566
};

Object properties have two addressing methods:

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
</head>
<body>
<script>
var person=
{
firstname : "John",
lastname  : "Doe",
id        :  5566
};
document.write(person.lastname + "<br>");
document.write(person["lastname"] + "<br>");
</script>
</body>
</html>

Run the program to try it


Undefined and Null

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

You can clear a variable by setting its value to null.

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
</head>
<body>
<script>
var person;
var car="Volvo";
document.write(person + "<br>");
document.write(car + "<br>");
var car=null
document.write(car + "<br>");
</script>
</body>
</html>

Run the program to try it out


Declaring variable types

When you declare a new variable, you can use 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;

Tips: JavaScript variables are all objects. When you declare a variable, you create a new object.




##Next Section

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>PHP中文网(PHP.CN)</title> </head> <body> <script> var carname1="我是双引号"; var carname2='我是单引号'; var answer1="这是正常的"; var answer2="我的名字是 'Johnny'"; var answer3='我的名字是 "Johnny"'; document.write(carname1 + "<br>") document.write(carname2 + "<br>") document.write(answer1 + "<br>") document.write(answer2 + "<br>") document.write(answer3 + "<br>") </script> </body> </html>
submitReset Code
ChapterCourseware