Learn JavaScript notes (1)-Basics

Source: Internet
Author: User

Here, just write down important things! This chapter is the foundation of the Foundation!

[Javascript]
<Script language = "javaScript">
// The undefined value is actually derived from the null value. Therefore, ECMAScript defines them as equal.
Alert (null = undefined); // output: true;
// Remove string Spaces
Var strings = "'Day to day '";
Alert (trimFunction (strings ));

/*
* Removes all spaces in the string.
*/
Function trimFunction (str ){
Return str. replace (/\ s/g ,"");
}
</Script>


[Html]
<Script language = "javaScript">
/*
* Determines whether a number is used.
* If the value is true, the variable value is not a number.
* If the value is false, the variable value is a number.
*/
Alert (isNaN ("fdsa "));
 
/*
* Obtain the length of a string.
*/
Alert ("saffdsa". length );
 
/*
* ToString () method of javaScript
*/
Var iNumber = 10.6;
Alert (iNumber. toString (2); // binary
Alert (iNumber. toString (8); // octal
Alert (iNumber. toString (); // default decimal
Alert (iNumber. toString (16); // hexadecimal
</Script>

 

Conversion of javaScript values:
[Javascript]
/*
* JavaScript numeric Conversion
* ParseInt/parseFloat
* The parseInt () method first checks the character at position 0 and determines whether it is a valid number. If not, NaN is returned
* Do not perform other operations. If yes, view the 1 character and perform the same test. This process continues until the characters of the invalid number are reached!
* In this case, parseInt converts the string before the character to a number.
* For example, if you want to convert the string "1234 bule" to a number, parseInt returns 1234 because when it detects B, the detection process is stopped,
* The number literal contained in the string is correctly converted to a number.
* If you want to convert to hexadecimal, or 8 or 2:
* ParseInt ("AF", 16); parseInt ("010", 8 );
*/
// Alert (parseInt ("1234 bule "));
// Alert (parseInt ("0xA "));
// Alert (parseInt ("22.5 "));
// Alert (parseInt ("bule "));
// Alert ("Convert:" + parseInt ("0010", 10 ));
// Alert (parseInt ("0 "));

/**
* Forced type conversion:
* Number (value) converts a specified value to a Number (which can be an integer or floating point Number)
* Boolean (value) converts the specified value to the Boolean type.
* String (value) converts a specified value to a String
* Use one of the three functions to convert the value and store the new value directly converted from the original value. This will cause abnormal noise!
*
*/
Alert (Number ("0010.23 "));
Number Type:
[Javascript]
/**
* Number class
* The toFixed () method parameter 2 indicates that the number of decimal places should be displayed. This method returns 99.00
* This method indicates a decimal number with 0 to 20 digits. If the value exceeds this range, an error is thrown.
*/
// Var numberObj = new Number (99 );
// Alert (numberObj. toFixed (2 ));

String type:
[Javascript]
/**
* String class
* The charAt (4) method returns the characters in the string whose index is 4 and the index starts from 0. This method returns "o"
* If you want to get a character code that is not a character, you can call charCodeAt (4). This method returns "111"
* Concat (string1, string2, stringn) This method connects one or more strings to the original value of the String object. The original String object remains unchanged.
* The first parameter of indexOf (searchValue, fromIndex) is the character to be searched, and the second parameter is located at the position of the string,
* The second parameter is optional. The default value of this method is 0 (starting with a string ).
* The lastIndexOf (searchValue, fromIndex) parameter is the same as that of the indexOf method!
* This method is used to start searching at the end of a character,
* Return values of two (indexOf () and lastIndexOf () methods:
* If the character is found, the position of the character string is returned. If the character cannot be found,-1 is returned.
* String truncation: slice (start, end), substring (start, end)
* Details reference: http://www.w3school.com.cn/js/jsref_obj_string.asp
* Case-insensitive conversion:
* ToLocaleLowerCase () converts a string to lowercase.
* ToLocaleUpperCase () converts a string to uppercase.
* ToLowerCase () converts a string to lowercase.
* ToUpperCase () converts a string to uppercase.
*
*/
Var stringObj = new String ("Hello World ");
Var resultObj = stringObj. concat ("string2", "string3", "stringN", "string5 ");
// Alert (resultObj );
// Alert (stringObj. charAt (4 ));
// Alert (stringObj. charCodeAt (4 ));
// Alert (stringObj. indexOf ("l", 7 ));
// Alert (stringObj. indexOf ("l "));
// Alert (stringObj. lastIndexOf ("l", 3 ));
// Alert (stringObj. lastIndexOf ("l "));
// Alert ("5" = 5); // outputs true

Array:
[Javascript]
/**
* Join ();
* The elements in the array are separated by any character.
* Split ();
* Convert a string containing special delimiters (any characters) into an array
* Split (separator, limit );
* Separator: A required parameter and delimiter.
* Limit: optional parameter, cutoff position, and last digit by default
* Concat is the same as the concat method of a string,
* All are added based on the original
* Slice (begin, end) is the same as a string, one is a string index, and the other is an array index.
*
*/
// Var colors = ["red", "blue", "green"];
// Alert (colors. join ("-string -"));
// Alert (colors. join ("-string-"). split ("-string -"));
// Var clo = "green ";
// Alert (clo. split ("", 2 ));
// Clo. split (separator, limit)
// Alert (colors. concat ("yellow", "purple "));
// Alert (colors. slice (1, 3 ));


/**
* Push (element1, elementN)
* This method is used to add one or more items to the end of an array.
* Pop ()
* This method is used to delete the last array (length-1) and return it as the function value.
* Shift ()
* In this method, the user deletes the first array item (0) and returns the value of [0 ].
* Unshift (element1, elementN)
* This method adds one or more items at the beginning of the array.
* Sort ()
* Encoding and sorting by character
*/
// Var stack = new Array ();
// Stack. push ("red ");
// Stack. push ("green", 2 );
// Stack. push ("blue", 1 );
// Alert (stack );
// Alert (stack. pop ());
// Alert (stack. shift ());
// Stack. unshift ("yellow", "purple ")
// Alert (stack. sort ());


Var spliceArr = new Array ("red", 2, 9, 4 );
/*
* Remove the items 0 to 2 from the array.
*/
SpliceArr. splice (0, 2 );
Alert (spliceArr );

/*
* Insert "blue", "yellow", "three" at location 2 to delete two items
*
*/
SpliceArr. splice (1, 2, "blue", "yellow", "three ");
Alert (spliceArr );

From Dan's column

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.