JS Experience 1__js

Source: Internet
Author: User
Tags array example lowercase natural logarithm reserved set time square root time and date

JavaScript syntax has strict capitalization, including keywords and variables ...

Unlike HTML in JavaScript, the newline space does not work. For example, the IF condition and alert must be wrapped.

Note: string in JS, you can use single quotes, or double quotes, recommended double quotes, special case only in single quotes.

Alert ("This is the first sentence \b  this is the second sentence")

td>

character

description

example

\b

Backspace

\f

Page breaks

Alert ("This is the first sentence \f This is the second sentence")

\ n

,
line break

Alert ("This is the first sentence \ n This is the second sentence. ")

\ r

Carriage return

Aler T ("This is the first sentence \ r This is the second sentence")

\ t

Tab

Alert ("This is the first sentence \ t This is the second sentence")

• Variable naming rules

– The first character must be a letter (both uppercase and lowercase), an underscore (_), or a dollar sign ($);

– Subsequent characters can be letters, numbers, underscores, or dollar characters;

– The variable name cannot be a reserved word;

– Character case sensitive;

The name of the hump is when the variable name or function name is the one or more words that are connected together, and the only word that is formed is the first word begins with a lowercase letter; the first letter of the second word, or the first letter of each word, is capitalized, for example: MyFirstName, Mylastname, such a variable name looks like camel peaks and so on, so named.

Note: The special constant value in JS: Undefined, when the variable is not initialized to be used, the value of the variable is undefined (undefined).

0. Implementing the first JavaScript case

• Need to note:

–javascript program content must be placed in the <script> </script> tab, type= "Text/javascript" is used to differentiate other scripting languages.

– for browsers that do not support JavaScript programs, the content between the tag <!--//--> is hidden, otherwise it will be displayed as HTML content, and it has no effect on the label for browsers that support JavaScript programs.

The –javascript program is sensitive to uppercase and lowercase letters, that is, in the same program statement if uppercase or lowercase characters are used to represent different meanings.

– Use note//Multiline comment///Single-line Comment

1. Built-in Functions

eval function: The value used to evaluate a string expression

This function can evaluate any valid JavaScript code that is represented as a string. The eval () function has a parameter, which is the code that wants to require a value.

var anexpression = "6 * 9% 7";

var total = eval (anexpression); Assign a variable total to 5

2.isNaN function: Used to verify that the parameter is NaN (not a number)

isNaN (NumValue)

Returns a Boolean value that indicates whether the supplied value is a reserved value NaN (not a number).

If the value is NaN, then the isNaN function returns True, otherwise it returns false

3. Number of parameters
When a function contains more than one parameter, Arguments.length can be used to get the number of arguments entered when the function is used, and the arguments includes the contents of each parameter.

• Note: Call a function with parameters, but not give it a value, function can run, or call a function without parameters, give it a value, the function is also running.

• Simple point: As long as you write the function name followed by a pair of parentheses, the function will run. So what about the parameters passed.

In fact, there is a parameter array object (arguments) in the function that encapsulates the passed arguments in an array.

Cases:

Functiondemo ()//defines a function.

{

alert (arguments.length);

}

Demo ("Hello", 123,true);//Call function.

The pop-up dialog box results in 3, and if you want to get all the parameter values, you can iterate through the array through a for loop.

In order to enhance the readability, it is best to pass the actual parameters according to the specification and the defined form parameters.

Other methods when the function is invoked:

Varshow = Demo ();//show variable receives the return value of the demo function.

Varshow = demo;//This notation is OK, meaning show and demo represent the same function.

The function can also be run by show ().

4. Built-in Objects

• Browser objects

• Custom Objects

• Note: An object must exist before it is referenced. In addition, there are two cases of references to object properties and methods in javascript:

– The object is a static object, which means that you do not need to create an instance of the object when referencing its properties or methods;

– When referencing the object properties and methods, you must create an instance of it, called a dynamic object.

5.string related functions:

• Attribute: Length

Method

–charat (Index): Returns the character at the specified index position.

–indexof (substring[, StartIndex]): Returns the character position for the first occurrence of a substring within a string object.

–substr (start [, length]): Returns a substring of the specified length starting at the specified position.

–substring (Start, end): Returns the substring at the specified position in the string object.

–tolowercase: Returns a string in which the letters are converted to lowercase letters.

6. Data types of variables and their conversions
JavaScript declares a variable without defining the data type, and therefore its variable is also known as the "no type" variable, that is, the declared variable name can be assigned to any type of data at any time, and JavaScript is automatically given the conversion.
var count=1;
...
Count= "The Count of Var is" +count;

• Returns the data type of an expression using the typeof () operator Var a=18;
typeof (a)---number

7.math objects:

name

Description

Property

PI

∏ value, approximately equal to 3.1415

LN10

10 of the natural logarithm of the value, approximately equal to 2.302

E

The value of the Euler constant, approximately equal to 2.718. Euler constants as the base of the natural logarithm

abs (y)

Returns the absolute value of Y

sin (y)

Returns the sine of Y, and the return value is in radians.

cos (y)

Returns the cosine of Y, the return value in radians

Tan (y)

Returns the tangent of Y, the return value in radians

min (x, y)

Returns the smaller number of x and y two numbers

Max (x, y)

Returns the larger number of x and y two numbers

Random

Returns a random number of 0-1

Method

round (y)

Rounded rounding

sqrt (y)

Returns the square root of y

Math.ceil (x) returns the smallest integer of the >=x

Math.floor (x) returns the largest integer <=x

Math.pow (x,y) returns the Y-second side of X

Math.random () returns a random decimal number between 0-1

Math.Round (x) returns rounded integers of x, rounded for a specific precision

8.date objects:

Method Grouping

Description

setxxx

These methods are used to set time and date values

getxxx

These methods are used to get time and date values

toxxx

These methods are used to return string values from the Date object

Parsexxx & Utcxx

These methods are used to parse strings

9. Array:

• declaring arrays

var array name = new Array (array size);

Example: var emp = NewArray (3)

You can also declare an array and assign an initial value:

Example: Var emp=new Array ("AA", "BB", "CC")

1) Emphasis on the definition of the keyword new– represents the new array-array

2 can declare an array and assign an initial value, and the size of the array is determined by the number of initialization data.

• How to refer to the form of elements: Objarr[index] To use an element in an array, index is the element in an array of indices, starting from 0 calculation

• The length property of an array: represents an array of lengths

• Multidimensional arrays: JS itself is not a multidimensional array concept, to be created by combining arrays

Two-dimensional array example 2:3*3
var arr = [[1,2,3],[4,5,6],[7,8,9]];

• Common Properties

Length: Returns the number of elements in an array

• Common methods

Method

Description

Join

To combine elements from an array into strings

Reverse

Reverses the order of the array elements so that the first element becomes the last, and the last element becomes the first

Sort

Sorting array elements

objarr.tostring (): Convert to string and use, connect:

–[1,2,3].tostring ()-> "1,2,3"

–["A", "B", "C", [1,2,3,4]].tostring ()-> "a,b,c,1,2,3,4"

Objarr.join (separator): Converts the separator as a delimiter, converting the array to a string, which is equivalent to ToString when the separator is a comma ()

Objarr.pop () returns the last element value of the array, note: This will also clear the element from the data, that is, Objarr.length minus 1

Objarr.push (V1,v2,...): Add parameters to the end of the array:

–[1,2,3,4].push ("A", "B")-> [1,2,3,4, "A", "B"]

objarr.shift (): Moves the first element of the array and returns the value

10 Summary:

array objects commonly used attributes are length, sorting method: Sort

• Loop statements are divided into: for loop, while loop, Do-while loop

The eval () function computes the value of a calculated expression.

The isNaN () function can be used to determine whether a number

The IndexOf () method of the string object is used to find the substring

The random () method of the math object can produce a random number of 0-1

The Date object has a setxxx () method to set the date and time, and the GetXXX () method is used to obtain the date and time

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.