Using freeCodeCamp, and starting back with the basics rather than trying to skip ahead and miss anything.
So, a very brief recap:
- Comments – work exactly like C# (// or /* */)
- Data types – seven of ’em: number, string, Boolean, symbol, object, null, undefined
- Variable declaration – dynamic e.g. var myVariable;
- Variable assignment – e.g. var a = 12; var b = a;
- Unintialised variables – e.g. var a; will have an initial value of ‘undefined’. This translates to NaN if numerical, or literal string of “undefined”.
- Case sensitivity – this != This, etc. camelCase is best practice
Working with numbers:
- Mathematical operators – as normal e.g. myVar = 1 + 2; var2 = 4 * 3; Also -, /, and % (remainder)
- Increment – as C#, e.g. i++ is the same as i = i + 1 (and similarly i– for decrement)
- Compound assignment – e.g. myVar = myVar + 5 can be written as myVar += 5 (similarly the operators -=, *=, and /= for subtraction, multiplication, and division respectively)
Working with strings:
- String literals – e.g. “my name” in var name = “my name”;
- Escaping literal quotes – to include quotes marks within a string using a backslash e.g. “I said, \”This is fun!\” .“;
- You can avoid having to escape literal quotes by using the other kind e.g. ‘I said, “This is fun!”.’ would produce the same result as above
- You can also escape other characters, e.g. backslash (\\), newline (\n), tab (\t), etc
- Concatenate strings with the plus operator (+), or append using the += operator.
- You can concatenate or append (string) variables into strings e.g. “My name is ” + myName;
- Length of a string – add .length to the end of a string e.g. “Hello”.length = 5
- Bracket notation – allows you to get the character at a specific index in a string. Nb: zero-based indexing, so count starts at 0 e.g. if var name = “Fred”; name = F, name = e, etc.
- Last character in string – combine length and bracket notation e.g. name[name.length – 1]. The -1 is needed because of that zero-indexing! Can also use this principle for the 2nd last letter (length – 2), etc.
- String Immutability – once created, string values cannot be altered. You CAN reassign the string! So var myStr = “Bob”; myStr = “Bub”; is fine, but myStr = “u” will not work!
Phew! Next up, arrays. All good revision, I’m sure!