tags: JavaScript

JavaScriptNotes

1. Understanding variables and console.log

1.1

var x = 10
console.log(x)

What is this stating?

  1. We are creating a variable, naming it x, and making it equal to 10
  2. Then we are logging to the console the value of x

1.2

var x = 10
x = x + 2
console.log(x)
  1. Creating a variable, naming it x, and setting it equal to 10
  2. Taking our variable and changing the value to x + 2 which in this case would read 10 = 10 + 2. Meaning now x is 12
  3. Logging out to the console the value of x. Since JavaScript is read top to bottom it will read the last value it was in this case 12

1.3

var x = "Hello"
var y = "World"
console.log(x)
console.log(y)
console.log(x+y)
console.log(x + " " + y)
  1. Creating a variable x and making it equal to Hello
  2. Creating a variable y and making it equal to World
  3. Logging out the value of x - Hello
  4. Logging out the value of y - World
  5. Logging out the value of x+y - HelloWorld
  6. Logging out the value of x + a space + y - Hello World

Why did x+y run all together? If we had declared another variable called z and made that x+y what would the result have been?

Use CodePen to test it live here are the examples
https://codepen.io/WolfsVeteran/pen/LYRKKom?editors=1012

1.4

function hello() {
    console.log('hello')
    return 15
}
var result = hello()
console.log("Dojo")
  1. Creating a function and calling it Hello
  2. when the function is called on print to the console 'hello'
  3. and Return the number 15 to the webpage only
  4. Creating a variable called result and calling on the function hello which will print in the console 'hello' and return the number 15
  5. logging out the word 'Dojo'

Back to ACC Main Notebook