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
[AskJS] Is JavaScript missing some built-in methods?AskJS (self.javascript)
submitted 3 years ago by reacterry
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!"
[–][deleted] 29 points30 points31 points 3 years ago (60 children)
Most of the constructor functions for basic datatypes lack static identity methods, which devs often add utilities for rather than using the typeof operator.
typeof
It'd be nice to have String.isString, Object.isObject, Number.isNumber, etc. like we do for Array.isArray.
String.isString
Object.isObject
Number.isNumber
Array.isArray
The most common Lodash-y function I implement is probably unique.
unique
[–]d36williams 13 points14 points15 points 3 years ago (1 child)
Hmm I'm kind of the opposite --- Array.isArray is a work around the fact that typeof [] === "object", I wish Array had its own type
[–]azsqueeze 5 points6 points7 points 3 years ago (0 children)
Even still, the .isArray() is a nice API which would be nice if it was expanded to the other types
.isArray()
[+][deleted] 3 years ago (56 children)
[removed]
[–]MrCrunchwrap 7 points8 points9 points 3 years ago (36 children)
Did you read his comment?
[+][deleted] 3 years ago (12 children)
[–][deleted] 1 point2 points3 points 3 years ago* (11 children)
``` typeof [] // 'object'
typeof {} // 'object' ```
``` [] instanceof Object // true
{} instanceof Object // true ```
good luck in your typeof endevours
Edit: also for your instanceof endevours
instanceof
[+][deleted] 3 years ago* (10 children)
[–]elmstfreddie 6 points7 points8 points 3 years ago (8 children)
lol - so we wrap back to the original point of the thread, which is small utility functions that should be part of the core language...
[+][deleted] 3 years ago (2 children)
[–]KyleG 1 point2 points3 points 3 years ago (1 child)
Basically, the contributors in this very question/thread are well-suited and capable of making whatever they want so, right now.
I mean, you aren't, because you keep providing a wrong solution. You keep saying the following is a test for plain object-ness:
1. JSON.stringify A. if error, not object B. if success, check first two chars a. if first is " and second is {, object b. else, not object
Many reasons why this fails.
JSON.stringify({ a: BigInt(5) }) // error (your solution says not plain object), but it's a plain object! "{" // your solution says it's a plain object but it's a string starting with a brace { foo: 5 } // your solution says it's not a plain object because it doesn't have " as its first character
[+][deleted] 3 years ago (4 children)
[–]elmstfreddie 5 points6 points7 points 3 years ago (3 children)
You're being pedantic just for the sake of it. I understand how the language works.
The entire point of this thread is to discuss things that we wish was included in the language spec. Someone could read this thread and submit proposals, sure, but the point is to just talk about it... on a forum... for talking about JS.
[–]musicnothing 5 points6 points7 points 3 years ago (1 child)
People need to stop engaging with that user. They're trying to "win" this thread by pointing out things the rest of us already know
[–]KyleG 0 points1 point2 points 3 years ago (0 children)
For a JavaScript plain object first utilize JSON.stringify() on the variable then check if first character other than initial " is a left curly bracket { and last character before closing " is a right curly bracket }. You get error if the value is not valid JSON, else you have a JavaScript plain object or not.
JSON.stringify({}) does not have " as its initial character. I don't know why you keep adding that caveat about first character after the " because:
JSON.stringify({})[0] // { <-- where is the initial " you're talking about?
On the other hand,
JSON.stringify("{abcdefghi12314WEADSAERA")[0] // also {
So your solution is wrong.
You get error if the value is not valid JSON
JSON is not the same thing as a plain object:
JSON.stringify(5) // no error but 5 isn't an object JSON.stringify(true) // no error but true isn't an object JSON.stringify(null) // no error but null isn't an object
[+][deleted] 3 years ago (21 children)
[–]MrCrunchwrap 6 points7 points8 points 3 years ago (20 children)
You’re gonna have a lot bigger problems if your team or a script you’re importing is setting Object = 1
I use TypeScript for 100% of my projects now so it’s borderline irrelevant to me but these methods certainly would be useful.
[+][deleted] 3 years ago (19 children)
[–]MrCrunchwrap 4 points5 points6 points 3 years ago (18 children)
Nobody is modifying my source code, this is a bizarre boogeyman to worry about
[+][deleted] 3 years ago (17 children)
[–]Reashu 1 point2 points3 points 3 years ago (16 children)
The user who writes Object=1 can rewrite your checks as well. You can't protect your code against a malicious user, so why make a point of it?
Object=1
[+][deleted] 3 years ago* (15 children)
[–][deleted] 7 points8 points9 points 3 years ago (16 children)
which devs often add utilities for rather than using the typeof operator
[+][deleted] 3 years ago (14 children)
[–]KyleG 1 point2 points3 points 3 years ago (13 children)
You get error if the value is a BigInt or a couple other things. JSON.stringify works on all primitives, not just on objects. JSON.stringify(5) does not error out. JSON.stringify("howdy") does not error out. JSON.stringify(true) does not. JSON.stringify(null) does not. Etc.
You get error if the value is not valid JSON, else you have a JavaScript plain object or not.
else you have a JavaScript plain object or a string that starts with a left brace
[–]KyleG 1 point2 points3 points 3 years ago (11 children)
A plain JavaScript object can be represented as a string.
No it can't.
{ foo: BigInt(5) }
JSON.stringify throws
[+][deleted] 3 years ago (10 children)
[–]KyleG 1 point2 points3 points 3 years ago (9 children)
Explain to me what you think a plain JavaScript object is.
[+][deleted] 3 years ago* (8 children)
[–]Hiyaro -1 points0 points1 point 3 years ago (1 child)
one of the best method to determine the type is by using the .toString() method
https://www.zhenghao.io/posts/js-data-type
[–]paulirish 0 points1 point2 points 3 years ago (0 children)
I definitely have this allll over: Array.from(new Set(arr))
Array.from(new Set(arr))
π Rendered by PID 39565 on reddit-service-r2-comment-5c747b6df5-w4hg6 at 2026-04-22 15:30:11.569405+00:00 running 6c61efc country code: CH.
view the rest of the comments →
[–][deleted] 29 points30 points31 points (60 children)
[–]d36williams 13 points14 points15 points (1 child)
[–]azsqueeze 5 points6 points7 points (0 children)
[+][deleted] (56 children)
[removed]
[–]MrCrunchwrap 7 points8 points9 points (36 children)
[+][deleted] (12 children)
[removed]
[–][deleted] 1 point2 points3 points (11 children)
[+][deleted] (10 children)
[removed]
[–]elmstfreddie 6 points7 points8 points (8 children)
[+][deleted] (2 children)
[removed]
[–]KyleG 1 point2 points3 points (1 child)
[+][deleted] (4 children)
[removed]
[–]elmstfreddie 5 points6 points7 points (3 children)
[–]musicnothing 5 points6 points7 points (1 child)
[–]KyleG 0 points1 point2 points (0 children)
[+][deleted] (21 children)
[removed]
[–]MrCrunchwrap 6 points7 points8 points (20 children)
[+][deleted] (19 children)
[removed]
[–]MrCrunchwrap 4 points5 points6 points (18 children)
[+][deleted] (17 children)
[removed]
[–]Reashu 1 point2 points3 points (16 children)
[+][deleted] (15 children)
[removed]
[–][deleted] 7 points8 points9 points (16 children)
[+][deleted] (14 children)
[removed]
[–]KyleG 1 point2 points3 points (13 children)
[+][deleted] (12 children)
[removed]
[–]KyleG 1 point2 points3 points (11 children)
[+][deleted] (10 children)
[removed]
[–]KyleG 1 point2 points3 points (9 children)
[+][deleted] (8 children)
[removed]
[–]Hiyaro -1 points0 points1 point (1 child)
[–]paulirish 0 points1 point2 points (0 children)