Function is not changing outer variable [duplicate] - javascript

This question already has answers here:
Does JavaScript pass by reference? [duplicate]
(13 answers)
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 2 years ago.
I'm new to Javascript
The function is not changing the outer variable
function edit(array, letter){
array = 0;
letter = 'b';
}
let string = 'a';
let array = [1,0,2];
edit(array, string);
console.log(array, string);// Result [1,0,2] not 0 b

Related

Javascript (function (){})() closure [duplicate]

This question already has answers here:
Javascript How to define multiple variables on a single line?
(10 answers)
What is the purpose of the var keyword and when should I use it (or omit it)?
(19 answers)
Closed 2 months ago.
Can any one explain why this happens?
// Example 1
(function () {
var a = b = 5;
})();
console.log(b); // Output 5
var a = b = 5 and var b = 5 is totally different?

Why does this.value return undefined but works fine if put inside a function? [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 11 months ago.
I'm learning javascript, I'm new to programming and i was wondering about "this".
let someObject = {
a : "happy",
b : "sad",
c : function(){
return this.a
},
d : this.a
}
someObject.c() returns "happy" but someObject.d returns undefined. why can't it simply assign the value of 'a' to 'd'?

How to call function after ".", javascript [duplicate]

This question already has answers here:
Add method to string class
(6 answers)
Closed 3 years ago.
I have this sample function, and i want to call it in this format :
const myfunc = (string) => {
return string.length
}
console.log("check_length".myfunc())
How can i do that?
Thanks for answers in advance!
The only way is to mutate the prototype of String with a classic function for accessing this.
String.prototype.myfunc = function () {
return this.length;
};
console.log("check_length".myfunc());

Function which refers to an object. Both are in an object. What does "this" mean? [duplicate]

This question already has answers here:
How does "this" keyword work within a function?
(7 answers)
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 6 years ago.
Nope, the title is not an enigma.
I have :
obj = {
NaNException:function(message=''){
this.message=message;
},
input:{
integer:function(a){
var b = prompt(a);
if (isNaN(parseInt(b, 10)))
throw new this.NaNException();
else
return b;
}
}
}
so, in NaNException:function(){ }, this is expected to be NaNException, but in integer, it is expected to be obj... which interpretation is the good one? And what can I do with the wrong one to have the expected result ?
Thanks in advance.

Is "var foo = []" the same as "var foo = new Array()"? [duplicate]

This question already has answers here:
What's wrong with var x = new Array();
(5 answers)
What is the difference between condensed arrays and literal arrays?
(3 answers)
Closed 6 years ago.
Is typing "var foo = []" the same as typing "var foo = new Array()"?

Categories