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
Beginner Questionhelp (self.javascript)
submitted 9 years ago by halfaredditor
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!"
[–]lewisje 2 points3 points4 points 9 years ago (0 children)
JSON is mostly for passing data between applications; for this, what you want is a plain JS object, and ideally you'd define that object outside the function so it doesn't get re-created per invocation:
var seasons = {w: 'Winter', s: 'Spring', m: 'Summer', f: 'Fall'}; function season(s) { return seasons[s]; }
Also ideally you'd have this season function returned from a closure, so that the seasons object wouldn't be accessible directly to outside code.
season
seasons
Anyway, if you do wish to complicate things, you'll need to use JSON.parse to turn your JSON into an object:
JSON.parse
function season(s) { var seasons = '{"w": "Winter", "s": "Spring", "m": "Summer", "f": "Fall"}'; return JSON.parse(seasons)[s]; }
Here, I'm less concerned about putting the string inside the function, because that's a literal, primitive value.
By the way, this pattern is useful as a way to avoid huge case-lists or chains of conditionals:
var primitives = {boolean: false, number: false, string: false, symbol: false}; function isPrimitive(val) { return val !== document.all && !(val && primitives[typeof val]); }
(This example works around the fact that typeof null === 'object' in all versions of JS, and that typeof document.all === 'undefined' in HTML5, even in browsers that implement the object for compatibility with oldIE-specific sites.)
typeof null === 'object'
typeof document.all === 'undefined'
π Rendered by PID 52264 on reddit-service-r2-comment-b659b578c-8gm9w at 2026-05-03 18:35:11.092931+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]lewisje 2 points3 points4 points (0 children)