JavaScript overview and basic knowledge points (variables, constants, operators, data types)

Source: Internet
Author: User
Tags arithmetic arithmetic operators

JavaScript Overview 1.1 What is javascript:

JavaScript (JS) is a client-side scripting language that is Object-and event-driven and has relative security. It is also widely used as a scripting language for client Web development, and it is often used to add dynamic functions to HTML pages, such as responding to various actions of users. Originally designed by Netscape (Netscape) 's Brendan Eich, it is a dynamic, weakly-typed, prototype-based language with built-in support classes. A scripting language that runs on the JS interpreter/engine interpreter. Because Netscape is working with Sun, Netscape Management wants it to look like Java, so it's named Javassript.

In the Web page, JS has two main implementation methods, one is to use <script> and </script> directly in the Web page , write JS language,<script> and </script in the middle of the label > tells JavaScript where to start and end. The second is to save the script to an external file, the file extension of the external JavaScript file is. js. Then use <script src= "myscript.js" ></script> introduce the external file, and set the JS path in the src attribute.

The main features of 1.2 JS are:
    1. JS is an explanatory scripting language (code is not precompiled);
    2. Mainly used to add interactive behavior to HTML pages;
    3. HTML pages can be embedded directly, but writing a separate JS file facilitates the separation of structure and behavior.
The main functions of 1.3 js are:
    1. Embed dynamic text in an HTML page.
    2. Responding to browser events
    3. Read and write HTML elements
    4. Validating data before data is committed to the server
    5. Detecting Visitor's browser information
    6. Control of cookies, including creation and modification, etc.
1.4 Complete JS consists of three parts:
    1. JS Core-ecmascript (ES)
    2. Document Object Model (dom:document)
    3. Browser object Model (Bom:browser)
1.5 Debugging for JS in the browser:

Usually we do not normally through the Nodejs--> independent installation JS script interpreter to the JS code interpretation run, most of the cases we use the browser embedded self-contained script solution to debug the JS code, that is, through the F12 to bring up the console debugging. The most commonly used is ——————console.log ().

Two. Simple JS syntax: 2.1 Variables and Constants (basic syntax in mind) 2.1.1 What is a constant:

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). The naming conventions for variables are as follows:

    1. Variables must start with a letter
    2. Variables can also start with the $ and _ symbols (although we do not recommend this)
    3. Variable names are case sensitive (Y and y are different variables)
    4. Hungarian Naming Act
      txt: text box, Password box, ...
      RDO: Radio button
      CHK: check box
      ...

    5. Underline naming method
      var _username;
      var userName;

    6. Hump Naming method
      Variable names consist of multiple words, all letters in the first word are all lowercase, starting with the second word, and the first character of each word becomes uppercase.
      var userName;

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

Declaration of the 2.1.2 variable:

Creating a variable in JavaScript is often called a "declaration" variable. We use the VAR keyword to declare the variable:

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

1 carname= "Volvo";

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

1  var carname= "Volvo";

Declarations can also span multiple lines:

1 var name= "Gates",2         age=56,3         job= "CEO";
View Code

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:

1 var carname;
2.2 Constants

Unlike the variable in JavaScript is the name of the constant we usually use the English letter capitalization, the constant keyword will also be different (const), once the assignment is immutable is the characteristics of the constant:

1 Const PI = 3.14159265;
JavaScript arithmetic operators

operator = is used to assign a value.
operator + is used to add values.

Arithmetic operators are used to perform arithmetic operations between variables and/or values.
Given y=5, these arithmetic operators are explained in the following table:

Note: The rules in the arithmetic operator are:
If you add a number to a string, the result becomes a string.
JavaScript Assignment operators
Assignment operators are used to assign values to JavaScript variables.

Given x=10 and y=5, the following table explains the assignment operator:

Three. Data types 3.1 JavaScript data types typically contain both primitive types and reference types: 3.1.1 Primitive types include:

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

(1) Undefined type
The Undefined type has only one value, which is Undefined. When the declared variable is not initialized, the default value of the variable is undefined.

1 var otemp;

The previous line of code declares the variable otemp, with no initial value. The variable is given a value of undefined, which is the literal of the undefined type. You can use the following code snippet to test whether the value of the variable equals undefined:

1 var otemp; 2 alert (otemp = = undefined);

This code displays "true", indicating that the two values are indeed equal. You can also use the TypeOf operator to show that the value of the variable is undefined:

1 var otemp; 2 alert (typeof// output "undefined"

Tip: The value undefined and differs from the undefined value. However, the typeof operator does not really differentiate between these two values. Consider the following code:

1 var otemp; 2 alert (typeof otemp);  // output "undefined" 3 alert (typeof OTEMP2);  // output "undefined"

The preceding code outputs all two variables as "undefined", even if only the variable OTEMP2 has never been declared. If you use an operator other than typeof for OTEMP2, it will cause an error, because the other operators can only be used on declared variables.
For example, the following code throws an error:

var= = undefined);

When the function has no explicit return value, the returned value is also "undefined", as follows:

function= = undefined);  // output "true"

(2) Null type

Another type with only one value is null, and it has only one private value, NULL, which is its literal. The value undefined is actually derived from the value null, so ECMAScript defines them as equal.

Alert (null = = undefined);  // output "true"

Although the two values are equal, they have different meanings. Undefined is a value that is assigned to a variable when it is declared but not initialized, and null is used to represent an object that does not already exist (this is briefly described when discussing the typeof operator). If the function or method is to return an object, the object returned is usually null when it is not found.

3) Boolean Type
The Boolean type is one of the most commonly used types in ECMAScript. It has two values of true and false (that is, two Boolean literals).
Even if false is not equal to 0, 0 can also be converted to false if necessary, so it is safe to use both in a Boolean statement.

1 var true ; 2 var false;

(4) Number type
The most special type defined in ECMA-262 is the number type. This type can represent both a 32-bit integer and a 64-bit floating-point number.
Any number that is entered directly (and not accessed from another variable) is treated as a literal of type #. For example, the following code declares a variable that holds an integer value, whose value is defined by the literal 86:

var iNum = 86;

(5) String type
What is unique about the String type is that it is the only original type that has no fixed size.
The following is a list of the character literals (escape characters) of ECMAScript:

3.1.2 Reference Types

Reference type Understanding: The exchange of variables is equal to the existing store key (variable reference address) copy one to the other boss, at this time two bosses at the same time to manage a shop, the behavior of two bosses can affect the operation of a shop.

 1  function   Chainstore () { var  store1=[' Nike China ' ];  3  store1;  4  alert (store2[0]); // nike China  5  store1[0]= ' Nike U.S.A. ' 

In the above code, Store2 only once assignment, theoretically its value is fixed, but later by overwriting the value of Store1, found that the value of Store2 has also changed, this is the characteristics of the reference type, but also we should pay attention to the place.

Because this document belongs to the children of the Moon (Night-drive), there is insufficient support and hope that everyone criticize correct ....

JavaScript overview and basic knowledge points (variables, constants, operators, data types)

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.