Reversing a variable in javascript [duplicate] - javascript

This question already has answers here:
How do you reverse a string in-place in JavaScript?
(57 answers)
Closed 8 years ago.
I don't really have a good way to explain in words what i wont to do.So im just going to have an example.
this is what the variable would be before.
var foo ="foo";
this is what i wont it be after.
var foo ="oof";
I hope that you under stand what i'm asking!
Thinks !

Try this:
var foo="start".split("").reverse().join("");

Related

Cannot understand this javascript statement [duplicate]

This question already has answers here:
Javascript || operator
(5 answers)
Javascript Shorthand - What Does the '||' Operator Mean When Used in an Assignment? [duplicate]
(6 answers)
JavaScript OR (||) variable assignment explanation
(12 answers)
Closed 3 months ago.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
I've been given a function by a client to create a custom script for tracking purposes which is fine, but being new to JS I'm having trouble understanding this line of code:
d[p]=d[p]||function(){(d[p].q=d[p].q||[]).push(arguments);};
function example is:
(function (d,e,k,u,p) {
d[p]=d[p]||function(){(d[p].q=d[p].q||[]).push(arguments);};
var a=e.createElement(k),b=e.getElementsByTagName(k)[0]; a.async=1;a.src=u;b.parentNode.insertBefore(a, b);
}(window,document,'script','https://www.example.com/bundle.js','stringname'));
If anyone could explain it that would be great
Just want to understand the statement better, it works fine and I can copy and paste with the correct url and string name but better if I understand the statement

What does ! mean in the context of react refs? [duplicate]

This question already has answers here:
In Typescript, what is the ! (exclamation mark / bang) operator when dereferencing a member?
(5 answers)
Closed 3 years ago.
I was looking through the react-data-grid source code and noticed this line: https://github.com/adazzle/react-data-grid/blob/34a2e51931bba2ac8a2f738cd059945d66516b48/packages/react-data-grid/src/HeaderCell.tsx#L107
return x - this.cell.current!.getBoundingClientRect().left;
I can't seem to figure out what the ! is doing.
That means that you are sure that variable value this.cell.current is not undefined or null

How do we find the length of an object in Javascript? [duplicate]

This question already has answers here:
Length of a JavaScript object
(43 answers)
Closed 6 years ago.
I dont see how we can find the length of an object. For arrays i can your array.length but it doesnt work for objects, any suggestions?
Thanks!
Just like that:
Object.keys(objectName).length;

Tricking Javascript variable [duplicate]

This question already has answers here:
Use dynamic variable names in JavaScript
(19 answers)
Closed 7 years ago.
I have a question that I could not find the answer, or perhaps cannot phrase the way it should...
I would like to trick javascript's way of handling variables...
Let's say in php I could do something like:
$test['usr_'.$id]=826
But when I try to do the same in Javascript/jQuery:
$("#usr_rank_h").val('rank_'+id);
It will output rank_826 instead of the value of the var rank_826
The equivalent idiom in javascript is actually
var id = 826;
var test = {};
test['rank_'+id] = 826;
Which gives you back an object of the form
{
'rank_826': 826
}
PS: I'm not sure why you are using jQuery in this case, are you getting the id from an input ?

How to write -webkit- in Javascript [duplicate]

This question already has answers here:
How to set the style -webkit-transform dynamically using JavaScript?
(5 answers)
Closed 7 years ago.
I need to change a property, which starts with -webkit-(and the other browser-specific statements), in Javascript. All of them start with dash. How am i supposed to write this?
In particular, the thing i need is -webkit-user-select. I know it should be something like webkitUserSelect, but no idea of what exactly.
Thanks
Just like you write any property that can't be used with dot notation:
element.style['-webkit-user-select'] = 'none';

Categories