r/learnjavascript • • Jul 16 '21

Javascript Basic Types

219 Upvotes

37 comments sorted by

13

u/tunelesspaper Jul 16 '21

Is there any good reason not to just always use backticks instead of quotation marks? I mean other than the fact that we’re used to using quotation marks.

22

u/hsnice Jul 16 '21

Personally, before your asking, I also believe in using back ticks always.

But as you ask, so I searched and found this "backtick literals are less performant (in other words, slower) because they need to processed. In other words, the variable interpolation that we want from backtick literals are an Achilles' heel — processing the template literals slows performance"

Hope! It will help you. Thankyou for asking.

6

u/tunelesspaper Jul 16 '21

Thank you for looking that answer!!

-3

u/inabahare Jul 16 '21

Don't thank him for that because he hasn't really told you anything. He hasn't told you his source and he hasn't told you the performance impact.

Remember, a fraction of a fraction of a second is still slower, but it doesn't mean anything.

-3

u/inabahare Jul 16 '21

Provide a source and not just a quote you may or may not have pulled from questionable places

4

u/camrn01 Jul 16 '21

There are performance implications as the other user suggests, but I also think it’s less readable at a glance.

4

u/alzee76 Jul 16 '21 edited Jul 16 '21

The variable interpolation from backticks provides a route for bugs in code to create security risks.

-1

u/inabahare Jul 16 '21

Sorry but that just seems like a "we need to fill a word quota" kind of section. Like

query = "SELECT x, y, z FROM Table WHERE id='" + id + "' "

"Oh no, string concatination can maybe possibly lead to bugs 😮😮😮😮😮"

0

u/alzee76 Jul 17 '21

Well yeah. Concatenation is bad too, but still better.

Bad: "SELECT x, y, z FROM Table WHERE id='" + id + "' "

Better: "SELECT x, y, z FROM Table WHERE id='" + some_sql_escape_function(id) + "' "

Best: "SELECT x, y, z FROM Table WHERE id=?", id (or with named params).

0

u/inabahare Jul 17 '21

Except your best example doesn't have anything to do with interpolation or concatination, and no concatination isn't better because this

"SELECT x, y, z FROM Table WHERE id='{some_sql_escape_function(id)}'" 

With interpolation would still be better than the first example. But this isn't a security issue with interpolation, as they seem to think

0

u/alzee76 Jul 17 '21

Except your best example doesn't have anything to do with interpolation or concatination,

It shows two examples of concatenation.

and no concatination isn't better because

Yes it is. Concatenation allows you to use an escape function as in my "better" example. Interpolation doesn't.

0

u/inabahare Jul 17 '21

Interpolation allows you to use functions as well my dude..

const someVar = `${someFunc(variable)} `

Like, its still more readable (especiallly if the value is a variable with a nice name) and securitywise its the same.

0

u/alzee76 Jul 17 '21

Now you're interpolating the function name. You've made a bad problem worse.

0

u/inabahare Jul 17 '21

You know you can press f12 and run it.. Right?

2

u/alzee76 Jul 17 '21

Haha what? God you're clueless. TTFN.

→ More replies (0)

11

u/Drugba Jul 16 '21

Cool slides, but I have two suggestions.

First, there six primitive types. You've left off BigInt and Symbol. I get why you did it, since this is for beginners and, personally, I've only used those once in a blue moon, but it might be better to say "there are four commonly used Javascript types", which allows you to leave those off without implying that they don't exist.

Second, I don't love the idea of squashing null and undefined into the same thing. They're different and I think one of the common mistakes I see beginners make is thinking they are just different names for the same thing. Since null has some weird behavior, I would have just left it off here and just had undefined

3

u/hsnice Jul 16 '21

Thanks for your suggestions, u/Drugba.

Yeah, I also personally have never use BigInt and Symbol so far. But thanks for your title "there are four commonly used Javascript types".

And thanks for sharing your idea on null and undefined.

5

u/vo0do0child Jul 16 '21

Cool slides. Would have been handy to learn as a beginner that a null value doesn’t crop up on its own and is usually an intentional absence of an object.

3

u/RoguePlanet1 Jul 16 '21

I've always understood it as "0" being a value, indicating the number of items like any other number, whereas "null" is the absence of any value at all.

Like if you want to indicate how many days off somebody took, and they don't respond, you can't put "0."

5

u/Robbiethemute Jul 16 '21
console.log(typeof null)
-> object

Uh oh...

2

u/[deleted] Jul 16 '21

Slides on Reference Types up next?

2

u/RoguePlanet1 Jul 16 '21

I'm a little thrown off by the positive vs negative infinity, it looks like the descriptions are switched. But it's probably because there's a dash used, which looks like a negative symbol.

2

u/Redditgaffx Jul 17 '21

Great deck, I would totally buy it if you cover the whole of JS. Looks so neat.

1

u/[deleted] Jul 16 '21

I think there are a few more data structures than this available in Javascript. I even wrote an article about it, though there are many articles and documents available, if you doubt me. https://www.mariokandut.com/javascript-data-types-and-data-structures/

1

u/[deleted] Nov 24 '21

Stolen content from Eloquent JavaScript

-1

u/xiipaoc Jul 16 '21

I guess object and function are not basic types? Also, null is an object.

WHICH IS STUPID, because I generally assume that if foo is an object, then I can check if 'bar' in foo, but I can't if foo is null, so I need to also do a null check. Not sure anything could be done about without massively breaking all existing code, unfortunately.

2

u/inabahare Jul 16 '21

The horror of having to write

obj && typeof obj === "object"

2

u/great_site_not Jul 17 '21

null is generally considered not to be an object--not just because it misbehaves as you're (rightfully) complaining about, but because it exists in memory as a tiny little primitive similar to an undefined or a number or a string, not a special annoying version of the sophisticated data structure of a JavaScript object.

the fact that typeof null == "object" isn't just misleading and stupid, it's actually a lie. and you already know why... it's for backwards compatibility. :(