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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:55:09+00:00 2026-05-26T10:55:09+00:00

if you compare different explicit methods of type-casting a variable to integer: var y

  • 0

if you compare different explicit methods of type-casting a variable to integer:

var y = parseInt(x,10) + 'text'; // too long, needs wrapping, needs anti-octal hack

var y = x.toFixed(0) + 'text'; // still long, and even uglier, and maybe buggy

var y = Math.floor(x) + 'text'; // long and uses Math object

var y = Number(x) + 'text'; // long

var y = +x + 'text'; // very short, but too hacky

var y = 1 * x + 'text'; // simple and short

You will see, why the last one is my favourite. Yet, i wonder, if there are any hidden issues with this method ?

  • 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-26T10:55:09+00:00Added an answer on May 26, 2026 at 10:55 am

    The last one does work:

    1 * 0.5; // 0.5
    

    if you want the best readiblilty use parseInt. And the radix is not a hack!

    Edit:

    My favorite:

    var y = x|0 + 'text';
    

    Edit: Please do note that this “trick” only works with 32-bit signed integers. Because that’s JavaScript’s implementation of it’s bit logic. So the largest positive number you can use this for is 2147483647.

    There is one unsigned bit operation, unsigned right shift. 0 >>> 1

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

Sidebar

Related Questions

I have to compare different versions of HTML pages for formatting and text changes.
I'm trying to compare two different object in JSF. A String and an Integer,
My Java application needs the ability to compare two different files in the filesystem
I just wanted to compare different solutions used when implementing ACLs in Rails.
How to compare two property files in two different folders using shell script input
I want to compare two CSV files to see if they are different. I
We want to compare general performance (CPU, I/O, network, ...) of different JVMs for
What is the difference between compare() and compareTo() methods in Java? Do those methods
I want to compare nodes of different edges in the graph. How can I
I have run into a situation where I need to compare two different lists

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.