Store extra properties in a javascript integer [duplicate] - javascript

This question already has answers here:
Javascript assigning value to primitive
(2 answers)
Closed 8 years ago.
Consider this excerpt from a node.js REPL session:
> var a = 5;
undefined
> a.b = true;
true
> a.b;
undefined
The intention is to store extra properties in a, which if successful would make the final line be true instead of undefined. How can I do this?

You cannot do that. A number is not an object in JavaScript. When you write:
a.b = true;
what happens is that the runtime instantiates a Number instance automatically. That's an object. You set the "b" property on that object to true, and then it's forgotten.
Now, you can do this:
var a = new Number(5);
a.b = true;
alert(a.b);
and it'll work just fine.

Related

how to use 'this" inside a singleton object in JavaScript properly? [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 1 year ago.
var a={b:44,c:this.b+1};
I want c to be b+1 or 45 in this case. but when I try this, I get Cannot read property 'b' of undefined error.
In JavaScript, when creating an object, the execution context doesn't change. Hence, the this value is picked up from the top-level in your case. Or to articulate in a different way, you can't access the properties of the object if it's not finished initializing yet.
The actual solution to your problem might be:
var a = { b: 44 };
a.c = a.b + 1;

Nodejs' try (check) for undefined [duplicate]

This question already has answers here:
Null-safe property access (and conditional assignment) in ES6/2015
(11 answers)
Closed 2 years ago.
In ruby we something like a.try(:property) even if a is nil(undefined) program won't throw an exception and keep going. But for nodejs/javascript if I have a.property and a is undefined program will throw an exception. I have to do something like
if (a) {
a.property
}
It is quite tedious and unpretty.
Is there something in nodejs similar like a.try(:property) so I don't have to check before I use a property?
I dont think there's any function like that in node.js. But you could build your own utility function like this...
// utility function
let tryProp = (a, p) => ( a? (a[p] ? a[p] : null) : null);
// Testing if property exists.
let x;
x = { key: "value"};
console.log(tryProp(x, 'john')); // returns null
console.log(tryProp(x, 'key')); // returns value
// Incase variable is undefined
let y;
console.log(tryProp(y, 'key')); // returns null

How JS engine distinguish null from undefined [duplicate]

This question already has answers here:
What is the difference between null and undefined in JavaScript?
(38 answers)
JavaScript checking for null vs. undefined and difference between == and ===
(8 answers)
Closed 4 years ago.
In below code snippet a==b return true, i.e they point to same memory location, hence they will have same value.
I would like to know, how JS engine knows a===b is false.
How is type information determined when 2 different types point to same memory location?
Edit 1: From comments it looks like my question might not be clear. I totally understand difference between == and === in terms of usage in JS language. I am more interested in knowing how JS engine saves type information for null and undefined. As per my understanding variables a & b point to same memory location that is why I get a==b, if this understanding is wrong, please correct me.
Edit 2: Ok I will put my question in another way. How typeof operator knows a is object and b is undefined despite having a==b.
var a = null;
var b = undefined;
console.log(a==b);
console.log(a===b);
console.log(typeof a);
console.log(typeof b);
In the code snippet added in the question:
var a = null; var b = undefined;
console.log(a==b);
console.log(a===b);
console.log(a==b) returns true because == uses type coercion to check the equality of both the variables. Therefore, null and undefined are thought of as equals.
console.log(a===b) returns false because === does not use type coercion. For ===, null and undefined are not same types and it doesn't care about checking deep equality when the operands aren't of the same type.
This has got nothing to do with memory locations.
a = null, b= undefined;
a == b /* only check their values */
a === b /* also check their types + values */
typeof a == typeof b // false
typeof "variable" gives the type of the variable.

Different ways of defining unassign variable in javascript [duplicate]

This question already has answers here:
How do I test for an empty JavaScript object?
(48 answers)
Closed 6 years ago.
I know when we want to define unassign variable in Javascript we can do:
var p;
and the other:
var p ={};
i want to know differences between these two ways, and if i define a variable in second way it is not null! what is the value in the variable, if we want using it in a if condition, for example :
var p ={};
if(p=='what i shout put there')
{}
var p is creating an unassigned variable. So console.log(p) will log undefined
var p ={}; is a way of creating object using literal notation.
Object p have methods like constructor,hasOwnProperty,toLocaleString etc
if(p=='what i shout put there'){}
If it is required to check if p is an object then below snippet is useful
if(Object.prototype.toString.call( a ) === '[object Object]'){
// Do rest of code
}
An object can have properties. like
var p={};
p.a ="someValue";
In this case you can check by
if(p.a === 'someValue'){
// Do rest of code
}
var p = {};
It is not unassigned ,it is infact assigned to the empty object
If you do below , it will be trut
if(p) {} // truthy

Javascript: String vs. Object [duplicate]

This question already has answers here:
Why does ("foo" === new String("foo")) evaluate to false in JavaScript?
(5 answers)
Closed 6 years ago.
I've looked all the questions and answers on stackoverflow, but couldn't find the simple answer to this.
What is exactly the difference between string and object?
For example, if I have this code:
var a = 'Tim';
var b = new String('Tim');
What exactly is the difference?
I understand that new complicates the code, and new String slows it down.
Also, I understand a==b is true, but going more strictly a===b is false. Why?
I seem to fail to understand the process behind the object and string creation.
For example:
var a = new String ('Tim');
var b = new String ('Tim');
a==b is false
a is of type string, whereas b is of type object.
=== includes typechecking and cause string is not an object
a === b will give you a false
new String ('Tim') === new String ('Tim') will evaluate to false too, because both are different objects
For normal strings there is no need to create an object, just create your variable and assign it a value.
And as far as your question regarding why == is true and === is false it's because:
== Compares values
=== Compares values AND type (One is a string, one is an object).
Another example of this is:
var a = 1;
var b = '1';
a == b //True as they both have the same value
a === b //false as one is a string and one is an integer
You can do the following to see the difference:
var a = "foo";
var b = new String("foo");
console.log(a);
console.log(b);
The first one is a string literal and the second is a String object. That's why when you compare them they are not equal but when you compare their values they are. You can read more about literals here.

Categories