Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6011439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:15:37+00:00 2026-05-23T02:15:37+00:00

Look at the 3 lines of code within this Javascript function. Assume that y

  • 0

Look at the 3 lines of code within this Javascript function. Assume that y will always be a String:

function example(x, y) {
    var s = {};
    s[y]  = x;
    return s;
}

Bearing in mind the following:

  1. Without wrapping it further within a function
  2. Without using ;

Is it possible to condense the 3 lines of code into one?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:15:38+00:00Added an answer on May 23, 2026 at 2:15 am

    Yes, with a little ugly code:

    function example(x, y, s) {
      return (s = {})[y] = x, s;
    }
    

    The extra parameter s is not passed into the function, it’s only there to be declared as a variable, so you don’t need the extra line var s;. (If you don’t declare it locally it becomes a global variable, which is bad practice.)

    The value of the assignment s = {} is what’s assigned, so you can make the assignment and then continue using the value in the expression.

    The comma operator returns the last value, e.g. (1,2) returns the value 2. That way you can add , s to the expression to make it return s.

    Edit:

    Another variation is using s as a variable in a for loop, and exit out of the loop:

    function example(x, y) {
      for(var s = {}; s[y] = x, true;) return s;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of XML that has lines that look like this <_char
Look at this: $('form).submit(function (event) { $(':input.required').trigger('blur'); var num = $('.warning', this).length; alert(num);//Focus here!
I've a big file with lines that look like 2 No route to specified
I have a data source with about 2000 lines that look like the following:
So, given that I have some type of data structure like this within a
I have a file with many lines in it. One of them look like
I was having a look at this tutorial at Sun on command line I/O.
Look at this image: alt text http://img139.imageshack.us/img139/4488/picture2ep3.png I know how to add UITableView with
Look at this situation: www.websitea.com displays an img tag with a src attribute of
I'm using dtSearch to highlight text search matches within a document. The code to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.