Web Development Technology--javascript Syntax 2 (variables, data types, objects)

Source: Internet
Author: User

JavaScript variables

A variable is a container for storing information.

Instance
var x=2;
var y=3;
var z=x+y;
It's like algebra.
x=2
Y=3
Z=x+y

In algebra, we use letters (such as x) to hold values (such as 2).

By the above expression Z=x+y, we can calculate the value of Z as 5.

In JavaScript, these letters are called variables.

tip: You can think of variables as containers for storing data.

JavaScript variables

Like algebra, JavaScript variables can be used to hold values (such as x=2) and expressions (such as z=x+y).

Variables can use short names (such as x and y), or they can use better descriptive names (such as age, Sum, totalvolume).

· Variables must start with a letter

· 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)

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

JavaScript Data types

JavaScript variables can also hold other data types, such as text values (name= "Bill Gates").

In JavaScript, a text like "Bill Gates" is called a string.

There are many types of JavaScript variables, but for now, we only focus on numbers and strings.

When you assign a text value to a variable, enclose the value in double or single quotation marks.

Do not use quotation marks when the value you assign to a variable is numeric. If you enclose a value in quotation marks, the value is treated as text.

Example
var pi=3.14;
var name= "Bill Gates";
declaring (creating) JavaScript variables

Creating a variable in JavaScript is often referred to as a "declaration" variable.

We use the VAR keyword to declare variables:

var carname;

After the variable declaration, the variable is empty (it has no value).

To assign a value to a variable, use the equals sign:

Carname= "Volvo";

However, you can also assign a value to a variable when you declare it:

var carname= "Volvo";
Example

In the following example, we create a variable named Carname and assign it a value of "Volvo" and put it in an HTML paragraph id= "Demo":

<p id= "Demo" ></p>
var carname= "Volvo";
document.getElementById ("Demo"). Innerhtml=carname;

tip: A good programming habit is to declare the required variables at the beginning of the code.

One statement, multiple variables

You can declare many variables in a single statement. The statement starts with VAR and separates the variables with commas:

var name= "Gates", age=56, job= "CEO";

Declarations can also span multiple lines:

var name= "Gates",
AGE=56,
job= "CEO";
Value = undefined

In computer programs, variables that are not valued are often declared. A variable that is not declared with a value, whose value is actually undefined.

After the following statement has been executed, the value of the variable carname will be undefined:

var carname;
Re-declaring JavaScript variables

If you re-declare a JavaScript variable, the value of the variable is not lost:

After the following two statements are executed, the value of the variable carname remains "Volvo":

var carname= "Volvo";
var carname;
JavaScript arithmetic

You can use JavaScript variables to do arithmetic, using the operators such as = and +:

Example
y=5;
x=y+2;
JavaScript Data types

String, number, Boolean, array, object, Null, Undefined

JavaScript has a dynamic type

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

Instance
var x//X is undefined
var x = 6; X is a number
var x = "Bill"; X is a string
JavaScript string

A string is a variable that stores characters, such as Bill Gates.

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

Instance
var carname= "Bill Gates";
var carname= ' Bill Gates ';

You can use quotation marks in a string, as long as you do not match the quotes enclosing the string:

Instance
var answer= "Nice to meet you!";
var answer= "He is called ' Bill '";
var answer= ' He is called ' Bill ';

You'll learn more about strings in the Advanced section of this tutorial.

JavaScript numbers

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

Instance
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:

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

You'll learn more about numbers in the Advanced section of this tutorial.

JavaScript Boolean

Boolean (logic) can have only two values: TRUE or FALSE.

var x=true
var y=false

Boolean is commonly used in conditional tests. You'll learn more about conditional testing in later chapters of this tutorial.

JavaScript arrays

The following code creates an array named cars:

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

or (condensed array):

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

or (literal array):

Instance
var cars=["Audi", "BMW", "Volvo";

The array subscript is zero based, so the first item is [0], the second one is [1], and so on.

You'll learn more about arrays in the chapters later in this tutorial.

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:

Instance
Name=person.lastname;
name=person["LastName"];

You'll learn more about objects in the chapters later in this tutorial.

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.

Instance
Cars=null;
Person=null;
Declaring 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;

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

JavaScript objects
    • · JS data type
    • · JS function

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

In JavaScript, objects are data that owns properties and methods.

Properties and Methods

A property is a value associated with an object.

Method is an action that can be performed on an object.

For example, a car is a real-life object.

Properties of the car:

Car.name=fiat
car.model=500
car.weight=850kg

Method of Car:

Car.start ()
Car.drive ()
Car.brake ()

The properties of the car include name, model, weight, color, etc.

All cars have these properties, but the properties of each car are different.

The method of car can be start, drive, brake and so on.

All cars have these methods, but they are executed in different times.

Objects in JavaScript

In JavaScript, objects are data (variables) that have properties and methods.

When you declare a JavaScript variable like this:

var txt = "Hello";

You have actually created a JavaScript string object. The string object has built-in property length. For the above string, the value of length is 5. A string object has several built-in methods at the same time.

Property:

Txt.length=5

Method:

Txt.indexof ()
Txt.replace ()
Txt.search ()

tip: in an object-oriented language, properties and methods are often referred to as members of an object.

In later chapters of this tutorial, you will learn more about the properties and methods of String objects.

Creating JavaScript Objects

Almost all of the transactions in JavaScript are objects: strings, numbers, arrays, dates, functions, and so on.

You can also create your own objects.

This example creates an object named "Person" and adds four properties to it:

Instance
Person=new Object ();
Person.firstname= "Bill";
Person.lastname= "Gates";
person.age=56;
Person.eyecolor= "Blue";

There are many different ways to create new JavaScript objects, and you can also add properties and methods to existing objects.

You will learn more about this in the chapters later in this tutorial.

Accessing the properties of an object

The syntax for accessing object properties is:

objectName. PropertyName

This example uses the Length property of the string object to look up the lengths of the strings:

var message= "Hello world!";
var x=message.length;

After the above code is executed, the value of X is:

12
Methods for accessing objects

You can call the method by using the following syntax:

objectName. MethodName ()

This example uses the toUpperCase () method of the String object to convert the text to uppercase:

var message= "Hello world!";
var x=message.touppercase ();

After the above code is executed, the value of X is:

HELLO world!
Did you know that?

tip: in an object-oriented language, functions that use the camel-case notation are very common. You will often see a function name such as SomeMethod () instead of Some_method ().

Web Development Technology--javascript Syntax 2 (variables, data types, objects)

Related Article

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.