r/learnjavascript • • May 23 '21

JavaScript Types

Post image
333 Upvotes

25 comments sorted by

7

u/Coolstreet6969 May 24 '21

JavaScript is loosely typed, making it extremely powerful.

So why was Typescript created in the first place then

2

u/posts_lindsay_lohan May 24 '21

There's no guaranteed way to eliminate bugs in software, you can only reduce their potential. TypeScript forces you to specify the data types that you are using - either custom created types by your team, or types that come from 3rd party libraries.

In doing this, you make it so that if there is an error, your code blows up right there in the editor and you see there is a problem that needs to be fixed before you can run the app.

Without TypeScript, you are blind to these problems. They will only occur when you run the app, and then, as you fix things, you have to hunt down all the places where things went wrong on each consecutive run of the app.

But perhaps its greatest benefit is when you have to do a large scale refactor. As soon as you move a big chunk of code, all the files that were dependent on it will light up so you know all the other places that need to be fixed before you can run your code successfully.

1

u/jaredcheeda May 25 '21

Though using TS to enforce types can help to reduce the amount of type-related bugs, a linter can effectively do this just as well, for a much lower cost (no slow compile times, no loss of access to non-typed 3rd party libraries, no additional onboarding/learning curve, no tooling restrictions).

Meta-studies have not found any difference in amount of bugs produced in codebases using strongly or weakly typed languages. That is to say, bugs occur at the same rate. The only measurable reduction occurs when strong testing practices are used. Language and typing have little impact. Turns out they're not the cause of bugs, humans are.

1

u/jaredcheeda May 25 '21 edited May 25 '21

The creator of C# wanted JavaScript to feel like C#, so he created TypeScript. This is fine if you like C#, but you are giving up JavaScript's true capabilities for it to imitate another language.

It's no secret that there are problems in JavaScript, and if you work with it long enough you will find solutions that work well for you to avoid these problems. TypeScript fixes a lot of these out of the box, but in a very C#/.NET/Java way, which is why those already familiar with JavaScript don't see any value in TypeScript. And why people coming from those other languages find it more appealing. It's closer to what they're already used to. This is also why TypeScript is sometimes called a "comfort blanket for backend devs". But to the point of solving problems for you:

"TypeScript is a lot of extra work to solve problems I don't have in ways I don't like." - Kyle Simpson, author of the extremely popular "You Don't Know JS" series.

Couldn't agree more with that quote. Further, if you would like to some day be good at JavaScript, starting with TS will just end up being a crutch that deprives you of the important experiences of solving these problems on your own. Experiences that lead to you better understanding the language and it's quirks and becoming a better developer.

2

u/queen-adreena May 24 '21

To appease coders who came to JS/Node from strongly typed languages because JavaScript was just too accessible and user-friendly.

0

u/UglyFrog420 May 24 '21

Because JavaScript is loosely typed, it is very easy to make certain kinds of errors which are only shown at runtime. Typescript eliminates the potential for these errors. It is also much easier to write code compatible with older browsers in Typescript.

3

u/Negative12DollarBill May 24 '21

Ironically, the phrase "JavaScript is loosley typed" has itself been loosely typed…

1

u/jaredcheeda May 25 '21

ya got me!

12

u/[deleted] May 24 '21

so i can use this to code minecraft?

JOKING

6

u/notAnotherJSDev May 24 '21

2

u/[deleted] May 24 '21

oh.

Thanks for sharing

5

u/hellracer2007 May 24 '21

unironically maybe

1

u/JollyWallaby May 24 '21

Technically, as long as you have access to a graphics library that supports 3D stuff with sprites, you can code Minecraft. This means you can build a clone using JavaScript, Python, Java, C++, C#, the list goes on :)

2

u/shiningmatcha May 24 '21

Want more cheat sheets like this one!

1

u/jaredcheeda May 25 '21

Alright, any particular topic?

0

u/[deleted] May 24 '21

[deleted]

3

u/shiningmatcha May 24 '21

what is wrong?

1

u/fieldOfThunder May 24 '21

NaN is useless and annoying. It should just throw an error if something results in NaN and force the dev to add a guard.

1

u/jaredcheeda May 25 '21

Yeah it doesn't add much value to the language, but at this point you can't remove it without breaking the internet. But at least it's easy enough to detect and throw if you wanted to.

function doThing (value) {
  if (isNaN(value) || typeof(value) !== 'number') {
    console.error('Value must be a number', value);
    return;
  }
  return value + 2;
}

2

u/fieldOfThunder May 27 '21

Indeed! I’d like to create a library that would be an opt-in to error throwing on NaN. Not sure where the best place to hook it in is but I’m sure it is possible.

2

u/jaredcheeda May 27 '21

I think a disallowNaN feature might fit well into this idea.

if you build it, let me know

2

u/shiningmatcha May 24 '21

So string is not an object, unlike Python where everything is object? And how about the String constructor?

0

u/[deleted] May 24 '21

[deleted]

3

u/posts_lindsay_lohan May 24 '21

You may be confused.

The ECMAScript specification lists string as a primitive: https://tc39.es/ecma262/#sec-primitive-value

4.4.5 primitive value member of one of the types Undefined, Null, Boolean, Number, BigInt, Symbol, or String as defined in clause 6

-4

u/Blueghost512 May 24 '21

I think null is an object

0

u/[deleted] May 24 '21

[deleted]

18

u/dvlsg May 24 '21 edited May 24 '21

Arguably, no.

https://developer.mozilla.org/en-US/docs/Glossary/Primitive

In JavaScript, a primitive (primitive value, primitive data type) is data that is not an object and has no methods. There are 7 primitive data types: string, number, bigint, boolean, undefined, symbol, and null.

(emphasis mine)

There are object wrapper equivalents, sure. But that doesn't mean they are always objects.


From the spec:

https://tc39.es/ecma262/#sec-terms-and-definitions-string-value

primitive value that is a finite ordered sequence of zero or more 16-bit unsigned integer values

Note that this is distinct from the "String object" section down a few lines from there.

https://tc39.es/ecma262/#sec-string-object

member of the Object type that is an instance of the standard built-in String constructor

...

A String object is created by using the String constructor in a new expression, supplying a String value as an argument.

0

u/[deleted] May 24 '21

[deleted]

10

u/thusman May 24 '21

new String('hello') creates a string object, 'hello' is a primitive string. If you call 'hello'.length JS wraps this value under the hood into a String object to make it work. You can see the difference with typeof new String('h') and typeof 'h'. Here is a great explanation too: https://programmingwithmosh.com/javascript/javascript-wrapper-objects/