About the definition of javascript variable [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What’s the difference between “Array()” and “[]” while declaring a JavaScript array?
var all = [];
var all = new Array();
What is the difference between these two definitions? I can't make them clear.

There is no difference. But best practice is to avoid using new on JavaScript primitive types.

Related

Best Way For naming Variables In JavaScript [duplicate]

This question already has answers here:
What is the difference between "let" and "var"?
(39 answers)
Closed 7 months ago.
Shall I use var , const , let in JavaScript when I want to declare variables..?
yes you can use let , var and const in javascript to declare variables

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 ?

Dynamic making Regexp [duplicate]

This question already has answers here:
Javascript Regexp dynamic generation from variables? [duplicate]
(4 answers)
Closed 7 years ago.
I'm looking for a working solution with dynamic regex in Javascript.
this solution works for me: (but is not dynamic)
new RegExp(\bal\i);
but this solution is not working:
var value = 'bal';
new RegExp('\'+value+'\i');
Could anyone help me how to adjust it to make it work? Thank you
you can pass the string (value) in the RegExp constructor, along with the ignoreCase flag as:
var value = 'bal';
var b = new RegExp(value, 'i')
b.test('BAL')
it returns true.

Reversing a variable in javascript [duplicate]

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("");

How can I tell the difference between a regular object and a jquery object (page element)? [duplicate]

This question already has answers here:
Check if object is a jQuery object
(9 answers)
Closed 9 years ago.
// var = {hey: "baby"};
// or
// var = $('#thingy');
if(typeof var == 'object'){ // this is true for both =(
}
I need my code to be smart enough to be able to tell the difference between these two. What's the best way to accomplish that?
You can use the instanceof operator for this.
obj instanceof jQuery
In addition, I don't think you want to use "var" as a variable name. It's reserved to establish variable scope.

Categories