Videos
Don't use
var str = new String();
Because
var str = new String("dog");
var str2 = new String("dog");
str == str2; // false
But
var str = "dog";
var str2 = "dog";
str == str2; // true
However, because of type coercion, the following works (Thanks to Rocket for pointing it out)
var str = new String("dog");
var str2 = "dog";
str == str2; // true
Single and double quotes don't matter, except for what quotes need to be escaped. Many others have noted that single quotes are better when creating HTML strings, since XHTML expects attributes to have double quotes, and you won't need to escape them.
When doing new String(), you are not being returned a string primitive, but a String object.
For all intents and purposes it acts like a string primitive, but there are cases where it won't. For example:
var a = new String('body');
jQuery(a); // This will *not* function as expected
// This will make a jQuery object containing a "String" object
// It will NOT treat it as a selector string
Also when comparing things, there may be problem. When you compare objects in JavaScript, it's only true if they are the same exact object, not just the same value. Example:
var a = new String('test');
var b = new String('test');
var c = a;
var d = 'test';
a === b; // false, a and b are different objects (a == b is also false)
c === a; // true, a is the same object as c
c === b; // false, c (which is a) is a different object than b
d === a; // false, d is a primitive and a is an object, they are not equal
'test' === d; // true, they are both string primitives
d == a; // true, this uses "==" so only value is compared, not type
You can use .valueOf() to convert a String object to a string primitive.
new String('test').valueOf() === 'test'; // true
So, I highly suggest using var a = '' or var a = "". As for single quotes vs. double quotes, there is no difference. Consider this example:
var a = "My name is Joe O'Ryan";
var b = 'My name is Joe O\'Ryan'; // need to escape the '
var c = 'Joe said: "Hi"';
var d = "Joe said \"HI\""; // need to escape the "
So, it's up to you whether to use ' or ", but I suggest those over new String().
You can take advantage of Template Literals and use this syntax:
`String text ${expression}`
Template literals are enclosed by the back-tick (` `) (grave accent) instead of double or single quotes.
This feature has been introduced in ES2015 (ES6).
Example
var a = 5;
var b = 10;
console.log(`Fifteen is ${a + b}.`);
// "Fifteen is 15.
How neat is that?
Bonus:
It also allows for multi-line strings in javascript without escaping, which is great for templates:
return `
<div class="${foo}">
...
</div>
`;
Browser support:
As this syntax is not supported by older browsers (mostly Internet Explorer), you may want to use Babel/Webpack to transpile your code into ES5 to ensure it will run everywhere.
Side note:
Starting from IE8+ you can use basic string formatting inside console.log:
console.log('%s is %d.', 'Fifteen', 15);
// Fifteen is 15.
Prior to Firefox 34 / Chrome 41 / Safari 9 / Microsoft Edge, nope, that was not possible in javascript. You would have to resort to:
var hello = "foo";
var my_string = "I pity the " + hello;
With Node.js v4 , you can use ES6's Template strings
var my_name = 'John';
var s = `hello ${my_name}, how are you doing`;
console.log(s); // prints hello John, how are you doing
You need to wrap string within ` (backtick) instead of ' (apostrophe)
Note, from 2015 onwards, just use backticks for templating
let a = `hello ${name}` // NOTE!!!!!!!! ` not ' or "
Note that it is a backtick, not a quote.
If you want to have something similar, you could create a function:
function parse(str) {
var args = [].slice.call(arguments, 1),
i = 0;
return str.replace(/%s/g, () => args[i++]);
}
Usage:
s = parse('hello %s, how are you doing', my_name);
This is only a simple example and does not take into account different kinds of data types (like %i, etc) or escaping of %s. But I hope it gives you some idea. I'm pretty sure there are also libraries out there which provide a function like this.
If it's a global variable then window[variableName]
or in your case window["onlyVideo"] should do the trick.
Javascript has an eval() function for such occasions:
function (varString) {
var myVar = eval(varString);
// .....
}
Edit: Sorry, I think I skimmed the question too quickly. This will only get you the variable, to set it you need
function SetTo5(varString) {
var newValue = 5;
eval(varString + " = " + newValue);
}
or if using a string:
function SetToString(varString) {
var newValue = "string";
eval(varString + " = " + "'" + newValue + "'");
}
But I imagine there is a more appropriate way to accomplish what you're looking for? I don't think eval() is something you really want to use unless there's a great reason for it. eval()