Analysis of C/c++,java,php,javascript,json array, object assignment when the last element can be followed by commas _javascript tips

Source: Internet
Author: User

1 c,c++,java,php can tolerate the comma at the end.

When an array is assigned to a C,c++,java, the comma at the end of the last element is optional. The following two lines of code are equivalent to these languages.

int a[] = {1,2,3}; /* correct
/int a[] = {1,2,3,}//* correct * *

PHP also inherits the features of C, and the following two lines of code are equivalent.

$a = array (1,2,3); /* Correct
/$a = array (1,2,3,);/* Correct */

2 JavaScript sees the end comma as a syntax error!

However, in JavaScript, the situation is very different, the last element must not have a comma at the end, otherwise it is a grammatical error.

var a = new Array (1,2,3); Correct
var a = new Array (1,2,3,);//Error

For an object, there is also no trailing comma.

var o = {name: ' Zhao ', age:12}; Correct
var o = {name: ' Zhao ', Age:12,};//Error

Although some browsers do it with the greatest tolerance after detecting this error, this is not a uniform behavior. IE series browsers do not tolerate such mistakes.

3 JSON also can't tolerate the comma at the end

{"Name": "Zhao", "Age": 12}//correct JSON format
{"name": "Zhao", "Age": 12,}//Bad JSON format

It should be noted that JSON is a common data format, regardless of the specific programming language. Various languages use varying degrees of tolerance when decoding JSON. PHP's Json_decode () cannot tolerate the comma at the end.

Json_decode ({"Name": "Zhao", "Age": 12,}); Parsing can cause errors

About the C/c++,java,php,javascript,json array, object assignment when the last element can be followed by a comma knowledge of the small series to introduce so many people, I hope to help you, for more information please login Cloud Habitat Community website to learn more!

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.