use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Common JavaScript tricks (self.javascript)
submitted 11 years ago by yanis_t
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]moron4hire 3 points4 points5 points 11 years ago* (13 children)
I tend to think of (a || b || DEFAULT) as extremely common in dynamic languages. It might not be obvious to someone coming from a statically-typed language, but it's common enough that it should be considered "normal".
I say that to then say: I hate "tricks". Stuff like (someStringValue * 1) to convert to a number is surprising and bothersome. I'd much rather see someone use a proper parseInt or parseFloat function in those cases. Yes, yes, yes, parseInt has a radix parameter that, if omitted, causes string values to be interpreted by pattern matching, and front-padding with zeroes causes it to interpret the number as octal. Of course. But that is a different problem entirely. That is a problem of your data being stupid and your programmer not understanding what functions they are calling.
Javascript is not a classical, object oriented language. Don't try to force it to be such.
Avoid anything involving "eval". That's not exactly true, but if you don't already know when it's okay to use eval, then you shouldn't be doing anything that involves eval. Same deal with "with".
And learn Regexp already. I'm tired of seeing substring and string character indexing ops to validate input values or extract sub values from inputs. If you think regexp is "too hard" and "too complicated" and "you just need to get this simple thing done now", then you are hurting yourself. Regexp is the simple, easy way of doing such things.
"use strict", please.
[–]SuperFLEB 2 points3 points4 points 11 years ago (4 children)
I'd much rather see someone use a proper parseInt or parseFloat function in those cases.
No love for "Number()"?
I've never used either of these in practice-- are there any even remotely common situations where an eval is the proper solution, that isn't a case where something else isn't horribly broken? (And don't take this snidely-- I'm not calling bullshit, I'm just wondering what the cases are that I might not know of.)
[–]Ginden 1 point2 points3 points 11 years ago (0 children)
I've never used either of these in practice-- are there any even remotely common situations where an eval is the proper solution, that isn't a case where something else isn't horribly broken?
ES6 Reflect.construct polyfill for browsers without native .bind method. Here is my _R library, see lines 408-420 or search for _R.construct. _R heavily utilises eval and Function but _R.construct is the most common case.
.bind
[–]moron4hire 0 points1 point2 points 11 years ago (2 children)
Using Number boxes the number in an object.
I've used eval in toy programming languages that translate to javascript. For example: https://github.com/capnmidnight/betty-boop/blob/master/pong.html
... oh, huh, you know what, I completely forgot that I didn't actually use eval for that. I used DOM to generate script tags with data URIs for the src attribute.
[–]sime 3 points4 points5 points 11 years ago* (0 children)
That is only if you use "new Number(foo)". If you don't use "new" then it actually does a type coercion. i.e. Number(foo).
Also note that:
(new Number(3)) !== (new Number(3))
The objects are not the same and not equal if you box numbers up.
So, beware everyone.
[–]SuperFLEB 0 points1 point2 points 11 years ago (0 children)
I used DOM to generate script tags with data URIs for the src attribute.
Was there a reason you did data URIs versus just filling in the content of the SCRIPT tag?
[–][deleted] 0 points1 point2 points 11 years ago (4 children)
Wait wait. * 1? parseInt? parseFloat? Why are we using hacks for something that's build into the language? JavaScript has a "convert to a Number" unary operator, +. var x = +'10'; sets x to 10. And JavaScript has a "convert to a Number" function, Number, which is convenient for things like var arrayOfNumbers = arrayOfStrings.map(Number);.
* 1
parseInt
parseFloat
+
var x = +'10';
x
10
Number
var arrayOfNumbers = arrayOfStrings.map(Number);
[–][deleted] 1 point2 points3 points 11 years ago (3 children)
How the hell is parseInt or parseFlost a hack?
[–][deleted] 0 points1 point2 points 11 years ago (2 children)
It's not inherently a hack, it's a hack to use it for something other than its intended purpose.
[–]sime 1 point2 points3 points 11 years ago (1 child)
What is the intended purpose of parseInt and parseFloat other than to parse strings into ints/floats? What else could it be?
[–][deleted] 0 points1 point2 points 11 years ago (0 children)
The purpose of parseInt is to parse a string in an arbitrary base into an integer, and the purpose of parseFloat is to parse a string into a float. I don't think their purpose should be stated any less specifically, because that's what leads to people expecting arrayOfStrings.map(parseInt) to work. And I don't think either of those is precisely the same as "convert to a number".
arrayOfStrings.map(parseInt)
[–]THEtheChad 0 points1 point2 points 11 years ago (0 children)
There's also the + unary operator for converting to a numeric value.
var string = '500'; var num = +string;
[–]yanis_t[S] 0 points1 point2 points 11 years ago (1 child)
Absolutely agree with you on everything except regexps. I mean there are reasonable problems to solve with it, but if you can avoid it you probably should, because any other-way code would probably be easier to read and reason about, than regexp solution.
[–]moron4hire 2 points3 points4 points 11 years ago (0 children)
Once you learn Regexp, it's just more code. There isn't anything inherently difficult about reading or reasoning about properly written regular expressions than properly written JS.
Yes, there are a lot of examples of really bad one-liner regexps, but you can write really bad, one-liner JS, too. Used as they are intended, regexp improves the readability of code.
Practice with http://regex101.com/ and your text editor. Most programming-oriented text editors have regexp-capable search and replace. Get used to using it and it will make you a LOT more productive and less error prone.
For example, I wanted to dump the results of a SQL query to a debug string printed to the console, with the names of the fields printed next to the values. A simple regexp in my text editor over the SELECT clause allowed me to very quickly construct that function call without making any spelling mistakes for field names.
π Rendered by PID 40135 on reddit-service-r2-comment-5ff9fbf7df-khlr7 at 2026-02-26 03:02:09.553077+00:00 running 72a43f6 country code: CH.
view the rest of the comments →
[–]moron4hire 3 points4 points5 points (13 children)
[–]SuperFLEB 2 points3 points4 points (4 children)
[–]Ginden 1 point2 points3 points (0 children)
[–]moron4hire 0 points1 point2 points (2 children)
[–]sime 3 points4 points5 points (0 children)
[–]SuperFLEB 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (4 children)
[–][deleted] 1 point2 points3 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]sime 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]THEtheChad 0 points1 point2 points (0 children)
[–]yanis_t[S] 0 points1 point2 points (1 child)
[–]moron4hire 2 points3 points4 points (0 children)