This question already has answers here:
What is the motivation for bringing Symbols to ES6?
(7 answers)
Closed 4 years ago.
I Googled a lot about new primitive type "Symbol" but still cannot understand actual use of it. What I understood is:
They are a primitive type, just as integer or string.
They are not a constructor function, hence you cannot use new Symbol() syntax.
But why is it used? String/Integer/Boolean have their uses which are clear, but what's the use of Symbol() exactly?
Moreover, for this code:
var data1 = Symbol();
var data2= Symbol('dummy');
why Symbol('dummy') === Symbol('dummy') is false?
This code Symbol('dummy') === Symbol('dummy') is false because each symbol is strictly unique.
Even if you use the same description (dummy), they are actually two different unique symbols.
They are used as identifiers.
You can read more about them here
Related
This question already has answers here:
How does primitive types in Javascript have methods and Properties? [duplicate]
(2 answers)
Closed last year.
Hello I'm new to JS and object-oriented programming in general.
I have two questions considering this
let arr = [1,2,3]
let a = 'hi'
When I run typeof(), arr is 'object' and a is 'string' right?
So my question is ,
When using arr.length to get the length of the array, what's the principle behind it? To be specific, I don't understand how I could get the length property though I've never initialized it. Does JS automatically set a length property value when we generate an object? How does it work?
Doesn't property only exist in objects? But why can we get the length of variable a using a.length? I thought objectname.property thing was for objects.
When you declare [1,2,...] you are declaring an object of class Array, which, in it's prototype, has a "length" property. Think of it as a variable that gets updated when you add or remove elements from the array.
https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/length
Strings are also objects in Javascript, and a string is also considered an array of characters, thus having the "length" property as well.
https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
This question already has answers here:
Rules for unquoted JavaScript Object Literal Keys?
(6 answers)
What characters are valid for JavaScript variable names?
(12 answers)
Closed 1 year ago.
I am seeing it crop up more and more in code I am going through on a new project (can't share due to contractual reasons) where Ill see something like:
{
prop1: value$ref,
$prop2: null
}
I have see ${prop3} before, but never an example without the brackets. Can anyone provide direction as to what the method is, or the operator is or whatever the case?
This question already has answers here:
What is the difference between JavaScript object and primitive types?
(1 answer)
Why to avoid creating objects of primitives in JavaScript?
(4 answers)
JavaScript primitive types and corresponding objects
(2 answers)
Closed 2 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
Are there any good reasons to use new Number(n)? Why does this constructor exist?
I can think of several reasons to avoid it, eg.,
literals are more memory efficient since they don't construct anything new.
literals are equal to each other (5 === 5 but new Number(5) !== new Number(5)).
the typeof operator indicates that it's a number rather than an object.
new Number(0) is never falsey. (!!new Number(false) == true).
You can use it for type conversion, but there are more concise methods. (+false === 0).
To be clear, I understand the Number object has useful methods and properties, my question is specifically about the constructor.
I'm using Number as an example, but I don't understand why Javascript exposes other constructors where literals are available either (String, Object, Array, etc.).
Why do these exist and are there ever any good reason to use them?
This question already has answers here:
Integer (literal) union type, with NaN as valid return value
(2 answers)
Closed 4 years ago.
Does TypeScript have a NaN type that I could declare for a variable?
For example, if I was to create a variable like this:
const number: number = parseInt('123', 10);
Could I change it to be more accurate, like so:
const number: number | NaN = parseInt('123', 10);
Basically I want to know if I can declare a variable as a NaN type (not undefined or null). The above snippet doesn't work in TypeScript and I can't find it listed in either the Basic or Advanced Types within TypeScript's guides.
There is no NaN type in Typescript. There is a proposal for this on GitHub but there is no timeline for implementation. Consider upvoting the issue if you want to see this feature in Typescript.
This question already has answers here:
What’s the difference between "Array()" and "[]" while declaring a JavaScript array?
(19 answers)
Closed 8 years ago.
What is the correct way on instantiate empty array in javascript?
var array=new Array();
var array=new Array;
var array=[];
All of them look same. Is there any difference?
var array=[];
works best.
Ref - http://jsperf.com/array-instantiation
There is one more way:
var array = Array() ;
They all will produce the same result, an empty object Array. I think the best way is using the array literal shortcut [] as it is what use less characters, it is what most people do.
Note that the same does not apply in the case of Boolean, Number and String, as those constructors will return primitives of type 'object' (object Boolean, object Number and object String respectively) while their literals will return primitives of types 'boolean', 'number' and 'string' respectively (and will behave differently in some situations), but array primitive type it is 'object' in any case.