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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:52:34+00:00 2026-06-03T23:52:34+00:00

I hope my logic isn’t flawed but I’m reading the Definitive Guide to JavaScript

  • 0

I hope my logic isn’t flawed but I’m reading the Definitive Guide to JavaScript and I don’t understand how this custom abs function works…

function abs(x) {
  if (x >= 0) {
    return x;
  } else {
    return -x;
  }
}

I recrafted it using a ternary operator in an attempt to understand it…

var res = (x >= 0) ? x : -x;
return res;

… but I still don’t get how it works.

Say I use -10 as x, how does it return +10? How does the sign reverse?

  • 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-03T23:52:35+00:00Added an answer on June 3, 2026 at 11:52 pm
    function abs(x) {
        if (x >= 0) {
    
            //If the number passed is greater than or equal to zero (positive)
            //return it back as is
            return x;
    
        } else {
    
            //If less than zero (negative)
            //return the negative of it (which makes it positive)
            // -(-10) === 10
            return -x;
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hope this isn't a waste of your time. I'm working on a project, and
I'm not even sure about how to title this qn. But, hope there is
I've been working at this all day so I hope I don't forget any
Odd question here not really code but logic,hope its ok to post it here,here
I hope this hasn't already been answered, but I feel like I have read
I hope this doesn't come across as a stupid question but its something I
I hope this question isn't too general. Well, the situation is that I am
hope all is swell! I was hoping I had cracked this problem but it
Hope I'm asking this correctly: I have a project Projects.Client I have my class
Hope this is the right place to be asking this, so my apologies if

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.