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 8829961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:57:22+00:00 2026-06-14T07:57:22+00:00

I often find that I write IF statements which immediately reference the value of

  • 0

I often find that I write IF statements which immediately reference the value of the conditional statement. For example, let’s say I need to check to see if a string matches a pattern:

if (mystring.match(/mypattern/) {
    var mymatch = mystring.match(/mypattern/)[1];
    ...
};

I suspect that what I’m looking for doesn’t exist, but I’ve wondered whether you can reference the conditional statement’s value within the if block, the way you can reference “arguments” within a function. In many cases, of course, I can rewrite it like this:

var mymatch = mystring.match(/mypattern/)[1];
if (mymatch) { ... };

But that’s often not possible if there’s a series of methods called. For example:

var mymatch = $('.myclass')[0].text().match(/mypattern/)[1];

… that would throw an exception if there were no item [0] on which to call .text(). Is there some convenient shorthand I’m missing out on? Or a better way to organize things? Just curious, really — I’ll go on living if the answer is no.

  • 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-06-14T07:57:24+00:00Added an answer on June 14, 2026 at 7:57 am

    In cases where relevant you can use the fact that the assignment operator returns a value in JavaScript, so for instance you can write things like:

    if (assignedTest = testedValue) {
       //value of assignedTest is now available 
       //and conditional will only be executed if true
    

    This could be used if the RHS was compatible or properly set-up but it’s also a huge readability concern since it’s very easy to confuse the assignment = with comparison ==/===.

    If you were particularly motivated to pursue this you could extract this type of functionality into a function that would behave in a reliable way: such as assigning the result of a closure to a named variable, and you could further tune the behavior to do other things (such as optionally evaluating to a different value within the test). Ultimately it would primarily be making a simple structure more complex though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When working with powershell cmdlets, I often find that I need to set a
I often find myself needing reference to an object that is several objects away,
When programming for the iPhone, I find that I often need to use the
When I write SQL queries, I find myself often thinking that there's no way
Often I find myself needing to write scripts that have to execute some portions
It seems that often I find that my code when moving either from one
When I load data in my code-behind, I find that often I want to
I often find myself with a file that has one number per line. I
I often find myself wanting to debug CSS layout issues that involve DOM changes
I often find myself with a list of disconnected Linq2Sql objects or keys that

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.