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

    PHP中文网(PHP.CN) 
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

    PHP中文网(php.cn) 

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

     

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:

    php中文网(php.cn) 

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.

    php中文网(php.cn) 

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.




##

Continuing Learning
||
PHP中文网(PHP.CN)
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!