Skip to main content

Quick overview


  • Define a variable. Example: let year = 2020
  • Use an existing variable. Example: year (assuming it was already defined)
  • Use functions to reuse logic in your code. For example ,the function sum that we defined
  • Using return inside a function which represents the result of the function
  • Using the following operators to do mathematical computations: + - * /

Strings


  • A string is a text, a set of characters that you may write to represent a name, city, country, description, product title, etc.
  • Strings have to start with a double quote " and end with one.
  • Forgetting the double quotes around strings is the most common mistake that beginners make. .length is a property of strings that returns the number of characters

Recap


  • A string is a text, a set of characters that you may write to represent a name, city, country, description, product title, etc.
  • Strings have to start with a double quote " and end with one.
  • Forgetting the double quotes around strings is the most common mistake that beginners make.
  • .length is a property of strings that returns the number of characters in a string.

Console.log


  • console.log() allows you to visualize your code in the console.
  • The console is only visible to you (the developer), but not the end-user.
  • console.log() helps you find bugs.
Don't do this:
function double(number) {
console.log(number * 2)
}

let result = double(4) // result is undefined

//or this
// THIS IS WRONG
function double(number) {
return console.log(number * 2)
}

let result = double(4) // result is undefined

Return


Return quits the function. It will be the last line of code executing in the function.

do this:

function double(number) {
console.log(number * 2)
return number * 2
}
let result = double(4) // result is 8 - we also see 8 in the Console

Watch out for hardcoding - don't just return 8 if the function is called to add 5+3.

Chapter recap


  • console.log() allows you to visualize your code in the console.
  • The console is only visible to you (the developer), but not the end-user.
  • console.log() helps you find bugs.
  • console.log does NOT replace return
  • console.log will never modify the result of a function; it will just show you a string or a number in the Console.
  • Sample usage is meant to show you examples of how your functions will be called.
  • We will often console.log the sample usage so that you can see its values in the console.
  • Beware of hardcoding - the sample usage is only meant as an example.