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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:12:07+00:00 2026-05-26T22:12:07+00:00

We know that the javascript logical operator || produces the value of its first

  • 0

We know that the javascript logical operator || produces the value of its first operand if the first operand is true. Otherwise, it produces the value of the second operand.

So in this example:

<script language="javascript">
function test (value){
    this.value = value || "(value not given)";
}
</script>

if the parameter value passed to the function is treated as false like the integer 0 or the empty string “” then this.value will be set to (value not given) which is not true correct (because indeed we are passing a value).

So the question is which should be the best way to set this.value?

EDIT: All 4 first answers use the ternary operator “?”. My question is about “||” operator.

  • 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-26T22:12:08+00:00Added an answer on May 26, 2026 at 10:12 pm

    The scheme with || is the most convenient to write, but it can ONLY be used when a falsey value (undefined, null, 0, "", false, NaN) is not a legitimate value. (When you just want to deal with null and undefined, you can use the new nullish coalescing operator (??) described in this proposal and included in ES2020.)

    If you want to allow specific falsey values and not allow others, then you have to write more specific code to handle your specific cases. For example, if you wanted to allow an empty string, but not allow null or undefined or other falsey values, then you’d have to write more specific code like this:

    function test(value) {
        if (value || value === "") {
            this.value = value;
        } else {
            this.value = "(value not given)";
        }
    }
    

    Or if you only want to exclude only undefined, you can test for it specifically:

    function test(value) {
        if (value === undefined) {
            value = "(value not given)";
        }
        this.value = value;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that JavaScript vars point to a value: var foo = true; //...
We all know that the logical && operator short circuits if the left operand
I know that javascript, for example supports functions inside of functions, like so: function
I know that in JavaScript, creating a for loop like this: for(int i =
I do know that in javascript, when you use this keyword inside a function,
I know that javascript doesn't have pointers in terms of a variable referring to
I know that in Javascript document.location.href = #my_id tells the browser to display the
I know that google's v8 compiles javascript into native machine (binary if I understand
I know that Visual Studio 2008 support JavaScript intellisense as I am using it
In JavaScript, I know that a closure is can be defined as a nested

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.