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

  • Home
  • SEARCH
  • 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 3661496
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:18:41+00:00 2026-05-19T01:18:41+00:00

I have been doing some reading lately one article I read was from Opera.

  • 0

I have been doing some reading lately one article I read was from Opera.

http://dev.opera.com/articles/view/javascript-best-practices/

In that article they write this:

Another common situation in JavaScript
is providing a preset value for a
variable if it is not defined, like
so:

if(v){
  var x = v;
} else {
  var x = 10;
}

The shortcut notation for this is the
double pipe character:

var x = v || 10;

For some reason, I can’t get this to work for me. Is it really possible to check to see if v is defined, if not x = 10?

–Thanks.
Bryan

  • 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-19T01:18:42+00:00Added an answer on May 19, 2026 at 1:18 am

    That Opera article gives a poor description of what is happening.

    While it is true that x will get the value of 10 if v is undefined. It is also true that x will be 10 if v has any “falsey” value.

    The “falsey” values in javascript are:

    • 0
    • null
    • undefined
    • NaN
    • "" (empty string)
    • false

    So you can see that there are many cases in which x will be set to 10 besides just undefined.

    Here’s some documentation detailing the logical operators. (This one is the “logical OR”.) It gives several examples of its usage for such an assignment.

    Quick example: http://jsfiddle.net/V76W6/

    var v = 0;
    
    var x = v || 10;
    
    alert( x ); // alerts 10
    

    Assign v any of the falsey values that I indicated above, and you’ll get the same result.

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

Sidebar

Related Questions

I have been doing some catching up lately by reading about cloud hosting. For
I love my GUID. However, lately I have been doing some research to understand
I have been doing a lot of unit testing lately with mocking. The one
I have been doing some reading and have found out that the Java Swing
I have been doing some reading, and I see that I can use getch()
I have been doing some reading in preparation for starting my first WCF project
I've been doing some reading recently and have encountered the Law of Demeter. Now
I have been doing some reading on this subject, but I'm curious to see
I have been doing some reading up on jquery live event and am still
I've been doing some reading into designing template code have a question about it.

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.