Solved

Why add + "" to end of  Math.random?

Posted on 2016-11-04
8
43 Views
Last Modified: 2016-11-05
I have no idea why the empty quotes were added to the end ?
                var axel = Math.random() + "",
                

Open in new window

Does anyone know why someone would do this?

Thanks
0
Comment
Question by:894359
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41875084
I dont think it is required. Remove and run the program again and see if you get any difference or issues?

It returns a A floating-point, pseudo-random number between 0 (inclusive) and 1 (exclusive).

Lot of examples are given @ - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
0
 

Author Comment

by:894359
ID: 41875123
I agree it is not require and removing it has no impact but I was wondering if there was some unknown reason for it?
0
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41875132
I must be a developer's mistake - i think unintentionally. :)
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 250 total points
ID: 41875148
It may be there to make sure the variable 'axel' is a string.  Since JavaScript sometimes does automatic conversions, some times you need to do something to make sure you get the type you want.
1
 
LVL 82

Assisted Solution

by:hielo
hielo earned 125 total points
ID: 41875150
Adding + "" at the end of the above statement essentially "casts" the random number to a string (as opposed to a number).  So, after the line you posted, if you were to write:
alert( typeof(axel) );

Open in new window

it should display string.  On the other hand, if you get rid of the + "", then the type would be reported as number.  Perhaps there was a reason why the original developer needed/wanted the random number as a string.
1
 
LVL 29

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 125 total points
ID: 41875153
Yes below methods can be used to convert a number to a string in JavaScript -
Pawan = Pawan+''      <<Implicit Conversion>>
Pawan = Pawan.toString()  <<Explicit Conversion>>

Although I don't like the first one since once the new developer comes it becomes difficult for him to understand the code.

Explicit conversion should always be used.
0
 

Author Closing Comment

by:894359
ID: 41875749
Thank you all!
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 41875764
You're welcome!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question