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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:40:22+00:00 2026-05-16T06:40:22+00:00

Why does if (prev = this.Prev()) { … } work but if (var prev

  • 0

Why does

if (prev = this.Prev()) {
    ...
}

work but

if (var prev = this.Prev()) {
    ...
}

does not? this.Prev() is a method for a Point object which returns a reference to a previous Point if it exists, and false if it does not. I don’t want to declare the variable to be global, and I don’t want something verbose like:

var prev = this.Prev();
if (prev) {
    ...
}

EDIT: What’s the most elegant way to do something like what I am trying?

  • 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-16T06:40:23+00:00Added an answer on May 16, 2026 at 6:40 am

    This happens because the if statement expects a expression:

    Syntax:

    IfStatement :
        if ( Expression ) Statement else Statement 
    

    var is a statement that’s why you get a SyntaxError.

    Your first example works because an assignment is a expression (AssignmentExpression)

    Edit:
    Let me quote this part:

    I don’t want to declare the variable to be global

    I understand your concern, an assignment made to an undeclared identifier may end up creating a property on the global object, moreover with the ECMAScript 5th Strict Mode, an undeclared assignment will cause a ReferenceError, breaking your code

    Variables in JavaScript are declared before the actual code execution, all occurrences of the var statement are bound to the current Variable Object, and they are initialized with undefined, you can’t really declare a variable conditionally.

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

Sidebar

Ask A Question

Stats

  • Questions 541k
  • Answers 541k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can have something like this: create table users (… May 17, 2026 at 2:55 am
  • Editorial Team
    Editorial Team added an answer I figured it out, but I have no idea why… May 17, 2026 at 2:55 am
  • Editorial Team
    Editorial Team added an answer Ok, I got around this by simply subclassing the built-in… May 17, 2026 at 2:55 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

i have a markup which look like this: <h3>Paragraf3-dummytext</h3> <p> <a name=paragraf3> Quisque id
I see output like this in my DMP file: Heap entries for Segment00 in
This one boggles my mind. I've used this same script (different targets of course)
if i have this html <div class=whole>This is a <div class=min>Test</div></div> i want to
The following code works just fine in IE, FF, Chrome and Safari, but I
Suppose I have a table like so: <table> <tr><td class=this-is-a-label>Label Cell</td></tr> <tr><td>Detail 1</td></tr> <tr><td
I have this site here I have an ajax request that calls a php
I have a window containing a form (formPanel). Users can show this window clicking
Does Ehcache 2.1 now support the transactional cache concurrency strategy in Hibernate 3.3.2GA? That
Does python have a full fledged email library with things for pop, smtp, pop3

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.