Latest web development tutorials
×

JavaScript Tutorial

JavaScript Tutorial JavaScript Introduction JavaScript usage JavaScript Output JavaScript grammar JavaScript Statement JavaScript Notes JavaScript variable JavaScript type of data JavaScript Object JavaScript function JavaScript Scope JavaScript event JavaScript The string JavaScript Operator JavaScript Comparison JavaScript If...Else JavaScript switch JavaScript for JavaScript while JavaScript Break & Continue JavaScript typeof JavaScript Type conversion JavaScript Regular Expressions JavaScript error JavaScript debugging JavaScript Variable promotion JavaScript Strict mode JavaScript Use errors JavaScript Form validation JavaScript Keep the keyword JavaScript JSON JavaScript void JavaScript Code specification

JS function

JavaScript Function definition JavaScript Function parameter JavaScript Function call JavaScript Closure

JS HTML DOM

DOM Introduction DOM HTML DOM CSS DOM event DOM EventListener DOM element

JS Advanced Tutorial

JavaScript Object JavaScript Number JavaScript String JavaScript Date JavaScript Array JavaScript Boolean JavaScript Math JavaScript RegExp Object

JS Browser BOM

JavaScript Window JavaScript Window Screen JavaScript Window Location JavaScript Window History JavaScript Navigator JavaScript Pop-ups JavaScript Timing events JavaScript Cookies

JS Library

JavaScript Library JavaScript test jQuery JavaScript test Prototype

JS Examples

JavaScript Examples JavaScript Object instance JavaScript Browser object instance JavaScript HTML DOM Examples JavaScript to sum up

JS Reference Manual

JavaScript Object HTML DOM Object

JavaScript data types

String (String), digital (Number), boolean (Boolean), array (Array), objects (Object), empty (Null), Undefined (Undefined).


JavaScript has a dynamic type

JavaScript has a dynamic type. This means that the same variables used for various types of:

Examples

var x; // x 为 undefined
var x = 5; // 现在 x 为数字
var x = "John"; // 现在 x 为字符串


JavaScript strings

The character string is stored (eg "Bill Gates") variables.

String can be quoted in any text. You can use single or double quotes:

Examples

var carname="Volvo XC60";
var carname='Volvo XC60';

You can use quotation marks in the string, as long as the match is not surrounded by quotation marks to the string:

Examples

var answer="It's alright";
var answer="He is called 'Johnny'";
var answer='He is called "Johnny"';

try it"

You will learn more about the string of knowledge in the advanced section of the tutorial.


JavaScript numbers

JavaScript is only one numeric type. Decimal point numbers can be without:

Examples

var x1=34.00; //使用小数点来写
var x2=34; // //不使用小数点来写

Large or very small numbers through scientific (exponential) notation to write:

Examples

var y=123e5; // 12300000
var z=123e-5; // 0.00123

try it"

You will learn more about digital knowledge in the advanced section of the tutorial.


JavaScript Boolean

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

var x=true;
var y=false;

Boolean conditions commonly used in the test. You will be in a later section of this tutorial learn more about the conditions tested.


JavaScript array

The following code creates the 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):

Examples

var cars=["Saab","Volvo","BMW"];

try it"

Array index is zero-based, so the first item is [0], the second is [1], and so on.

You will be in a later section of this tutorial learn more about the array.


JavaScript Object

Objects are separated by braces. Within parentheses, property of the object name and value pairs (name: value) is defined. Property separated by commas:

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

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

Spaces and wrap irrelevant. Statement may span multiple lines:

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

Addressing object properties in two ways:

Examples

name=person.lastname;
name=person["lastname"];

try it"

You will be in a later section of this tutorial learn more about the object of knowledge.


Undefined and Null

Undefined This value represents the variable does not contain a value.

You can set the value of the variable to null to clear the variables.

Examples

cars=null;
person=null;

try it"


Declare variable types

When you declare 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;

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