A Guide to Discord Bots
What's The Difference Between...
"a", 'a' and `a`
There's no difference between them: they are all strings.
However, you can use variables more conveniently with ``.
let variable = "World";
let strA = "Hello, " + variable + "!";
let strB = 'Hello, ' + variable + '!';
let strC = `Hello, ${variable}!`;
But, depending on which one you choose, you might need to escape some characters using \
.
Escaping a character will print as it is, without considering it as the start/end of a string for example.
let strA = "Let's see... What's a \"variable\"?";
let strB = 'Let\'s see... What\'s a "variable"?';
let strC = `Let's see... What's a "variable"?`;
Note: in JSON, name: 'John'
is invalid while name: "John"
is valid.
let, var, const
- Const declares a variable globally, and its content cannot be changed.
- Var declares a variable globally
- Let declares a variable in its block scope and sub-blocks (function, try, switch, if, etc.)
A 'block' is defined by braces/curly brackets { }
.
const a=1; // accessible everywhere
var b=2; // accessible everywhere
let c=3; // accessible in all sub-blocks, which is everywhere
function calc() => {
var d=4; // accessible everywhere
let e=5; // accessible only inside calc()
console.log(a, b, c, d, e, f); // 1, 2, 3, 4, 5, undefined
}
if (a==1) {
let f=6; // accessible only inside 'if'
console.log(a, b, c, d, e, f); // 1, 2, 3, 4, undefined, 6
}
console.log(a, b, c, d, e, f); // 1, 2, 3, 4, undefined, undefined
for, .forEach(), for x in y
Technically, you can sometimes use any of them, but:
- for can make use of variables and conditions
- You can't use break nor continue with forEach()
- for x in y can be used to go through objects
let array = ['one', 'two', 'three', 'four', 'five'];
// This loop...
array.forEach(string => console.log(string));
// ... and this one
for (let string in array)
console.log(string);
// ... and this one
for (i = 0; i < array.length; i++)
console.log(array[i]);
// ... are all equal
str.includes() and str.indexOf() > -1
- The includes method finds NaN and undefined whereas the indexOf() method doesn't.
- The includes() method does not distinguish between -0 and +0.