TIL 2: javascript 1


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!\” .“;
  • Nb single quotes are also valid in javascript, unlike most languages – just be consistent
  • 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[0] = F, name[2] = 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[1] = “u” will not work!

Phew! Next up, arrays. All good revision, I’m sure!


4 responses »

  1. Pingback: TIL 6: javascript 2 | diary of a scanner

  2. Pingback: TIL 9: javascript 3 | diary of a scanner

  3. Pingback: TIL 13: javascript 4 | diary of a scanner

  4. Pingback: TIL 16: javascript 5 | diary of a scanner

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s