Toggle boolean using function? [duplicate] - javascript

This question already has answers here:
Does JavaScript pass by reference? [duplicate]
(13 answers)
Closed 6 years ago.
I would like to create a single function to toggle any boolean of my code, but I'm not able to get the expected result :
function toggle(x){
x=!x;
}
var test=false;
toggle(test);
alert(test);
Why doesn't this return true ?

Boolean datatype is passed by value. So, any changes made to the argument x will not reflect the actual variable test. You need to return the updated value from the function and assign it back to the variable.
function toggle(x) {
return !x; // Return negated value
}
var test = false;
test = toggle(test); // Assign the updated value back to the variable
alert(test);
But, as said in comments, it's better to use
test = !test;

Related

String passed as function parameter is undefined [duplicate]

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 6 years ago.
When I pass a string to a function as a parameter, it returns undefined. Why is that?
let a = 'b';
let test = (a) => console.log(a);
test(); // undefined
When you call test(); you aren't putting anything between ( and ), so you aren't passing any parameters.
When you defined test (with (a) =>) you created a local variable a that masks the global variable with the same name.
To pass a you have to actually pass it: test(a).
Why is that?
Because you don't pass any argument. Try the following:
test(a);
The following definition:
let test = (a) => console.log(a);
is like the following:
function test(a){
console.log(a);
}
So when you call test, without passing any argument the value of a would be undefined.

Angular: Change String to variable [duplicate]

This question already has answers here:
Convert string to variable name in JavaScript
(11 answers)
Closed 6 years ago.
Apologies if this is simple. I just couldn't figure-out how to do it. Searched on this site on how to dynamically change variables and evaluate them etc but can't figure out how to do what I want.
Problem: I have a variable that I set to a value:
vm.toggleDrop = function($switchToggle){
vm.switchValue = "switch"+$switchToggle;
//Here
};
Where it says "Here", I need to instantiate a new variable that is called whatever is the result if the above statement eg; switch1 then set it true or false as a boolean. eg: switch1 = false;
Therefore again, if $switchToggle parameter was "Test", I need to dynamically create a variable called switchTest and set it true or false.
Is such a thing possible ?
Thanks all
Something like that?(I created vm variable just for code snippet, ignore it)
var vm = {};
vm.toggleDrop = function($switchToggle){
vm.switchValue = "switch"+$switchToggle;
vm[vm.switchValue] = 'whatever';
console.log(vm);
};
vm.toggleDrop('true');
Also if you do not need to attach variable to vm object, best answer will be #nastyklad provided(using window[vm.switchValue]
Something like this:
function setVariable(name){
var variableName = 'switch' + name;
vm[variableName] = true;
}

Change variable through function in JavaScript [duplicate]

This question already has an answer here:
Changing variable in another function in JavaScript
(1 answer)
Closed 7 years ago.
I am trying to automate the changing of a variable through a function. However although it returns the right value, it doesn't change the actual value of the variable passed to it.
function change(one, two){
one = two;
return one;
}
var test = 1;
change(test, 5);
// returns 5;
console.log(test);
// still 1
Why does this happen and how can I solve this?
You can't pass variables. When you call change(test, 5); you are passing the value of test not the variable test.
That value is copied to the variable one.
You then assign a new value to one, but that doesn't touch test.
If you want to do anything like this, you need to pass an object and then modify the value of a property of the object.
function change(one, two){
one.test = two;
return one.test;
}
var myObject = {
test: 1
};
change(myObject, 5);
console.log(myObject.test);
You can't do this in JavaScript. Variables cannot be passed by reference, they are always passed by value.

Is it possible to make User defined DOM changes in runtime? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
javascript object, access variable property name?
I am messing around with JS, learning a stuff and I am wondering about something...
Say you have a function aFunc(), and you accept a string aFunc(val). The value is user defined and is then used to modify the CSS of an element.
For Example:
function aFunc(val){
document.getElementById('something').style.val = 'red';
}
Say the user entered borderColor, it would somehow refrence borderColor where val is. I do not know how or if this is possible.
EDIT:
Please no eval() :)
Just use this as a base: JSBIN- Demo on a Div
var type = prompt("style");
var value = prompt("value");
document.body.style[type] = value;
Every object in JavaScript has a method called hasOwnProperty which takes a string value and will return a Boolean value.
var myObj = {
name: "Josh"
};
myObj.hasOwnProperty("name") === true; //This is true
You can use that to test for the presence of a particular property and then use the method stated in Akhil Sekharan's answer to access that property.

Get the 'name' of a variable in Javascript [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Determine original name of variable after its passed to a function.
I would like to know if its possible to get the actual name of a variable.
For example:
var foo = 'bar';
function getName(myvar) {
//some code
return "foo"
};
So for getName(foo) will return "foo"
Is that possible ?
Thanks.
I don't think it is possible. When you call a function you pass an object, not a variable. The function doesn't care where the object came from.
You can go the other way though if you call your function as follows:
getName('foo')
Or pass both the value and the name:
getName(foo, 'foo')

Categories