C/c++,java,php,javascript,json array, object assignment, can the last element be followed by a comma?

Source: Internet
Author: User

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

When assigning a value to an array in 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};      /* 正确 */int a[] = {1,2,3,};     /* 正确 */

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

$aarray(1,2,3/* 正确 */$aarray(1,2,3/* 正确 */
2 JavaScript view end comma is 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 syntax error.

varnewArray(1,2,3//正确varnewArray(1,2,3//报错

For an object, you also cannot have a comma at the end.

var o = { name:‘赵‘, age:12// 正确var o = { name:‘赵‘, age:12// 报错

Although some browsers implement the maximum tolerance after detecting this error, this is not a uniform behavior. IE series browsers do not tolerate this error.

3 JSON also does not tolerate the comma at the end
{"name":"zhao""age":12}   // 正确的JSON格式{"name":"zhao""age":12,}  // 错误的JSON格式

It is important to note that JSON is a common data format that is independent of the specific programming language. Various languages also use different levels of tolerance when decoding JSON. PHP Json_decode () will not tolerate the comma at the end.

json_decode({"name":"zhao", "age":12,})// 解析会发生错误

C/c++,java,php,javascript,json array, object assignment, can the last element be followed by a comma?

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.