10 Mini Tips for JavaScript beginners 1th/2 page _javascript tips

Source: Internet
Author: User
In the previous list of programming languages, we introduced the JavaScript language of the impending regularization, as illustrated in the article, that JavaScript is not only the most dynamic scripting language, but also one of the most useful programming languages. Because the vast majority of browsers are compatible with it, you can use it in these browsers. JavaScript is accepted fairly quickly because it is so simple and has a fairly wide range of uses. Many programmers used to think of JavaScript as a "toy language", but Ajax came into the market with the opposite side, allowing JavaScript to exhibit completely different capabilities and capabilities.
As a result of this invention, programmers can now create Web applications with desktop application effects, which is useful because the data can be changed more quickly. Here are some mini tips that can help beginners better use JavaScript. JavaScript is a wide range of uses, and there are so many styles, so it can have a lot of tricks. In addition, although it is a lot of programming methods, but I only selected 10 techniques, I think these skills for beginners to understand JavaScript is a good starting point.
1, add an element at the end of an array
This technique allows you to add an element to the end of an array using the Length property, because the length property is 1 more than the subscript of the last element of the array. This method is the same as the "push" method. For example:
Copy Code code as follows:

var myarray = [];
Myarray[myarray.length] = ' New Element ';

2, adjust the length of an array
The Length property is not read-only, so you can set the value of the length property. Also, you can use it to increase or decrease the length of an array. For example:
Copy Code code as follows:

var myarray = [1,2,3];
Myarray.length//3
Myarray.length = 2; Delete the last element
Myarray.length =//Add elements to the array; The elements have the undefined value.

3, use "!!" Convert any data type to Boolean
This technique allows you to use "!!" Converts any data type (such as String, number, or integer) to a Boolean. For example:
Copy Code code as follows:

var myString = ' 23255 ';
typeof MyString; String
MyString =!! myString;
typeof MyString//boolean

4, convert number to string
This technique allows you to add an empty string at the end of number to convert number to string, for example:
Copy Code code as follows:

var mynumber = 234;
typeof MyNumber; Number
MyNumber + = ';
typeof MyNumber; String

5, understand how many variables a function requires
This is a great technique that allows you to know exactly how many variables a function needs. For example:
Copy Code code as follows:

function Add_nums (NUM1, num2) {
return NUM1 + num2;
}
Add_nums.length//2 is the amount of parameters expected by the function add_nums

6, use the "arguments" object to see how many parameters a function receives
This technique allows you to use the "arguments" object to see how many parameters a function receives. For example:
Copy Code code as follows:

function Add_nums () {
return arguments.length;
}
Add_nums (23,11,32,56,89,89,89,44,6); This is return the number 9

This technique is useful when you need to check the validity of the number of parameters, or when you need to create a function that has an indeterminate number of parameters.
Copy Code code as follows:

function Sum_three_nums () {
if (arguments.length!=3) throw new Error (' received ' + arguments.length + ' parameters and should work with 3 ');
}
Sum_three_nums (23,43); Return to the error message
function Sum_num () {
var total = 0;
For (Var i=0;i<arguments. length;i++) {
Total+=arguments[i];
}
return total;
}
Sum_num (2,34,45,56,56);

Current 1/2 page 12 Next read the full text

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.