3
u/Negative12DollarBill May 24 '21
Ironically, the phrase "JavaScript is loosley typed" has itself been loosely typed…
1
12
May 24 '21
so i can use this to code minecraft?
JOKING
6
u/notAnotherJSDev May 24 '21
I know you’re joking, but you actually can.
simondev on YouTube did it as an experiment. He’s also updated the video
2
5
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
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
disallowNaNfeature 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
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
0
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
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'.lengthJS wraps this value under the hood into a String object to make it work. You can see the difference withtypeof new String('h')andtypeof 'h'. Here is a great explanation too: https://programmingwithmosh.com/javascript/javascript-wrapper-objects/
7
u/Coolstreet6969 May 24 '21
So why was Typescript created in the first place then