01 - JavaScript Fundamentals
01 - JavaScript Fundamentals
Output
console.log("Hello world!")
console.log("This is the way ")
// Output:
// Hello World
// This is the way
const abc = 15
console.log(abc) // outputs 15
console.log(15+20/5) // outputs 19
console.log("bye!") // outputs bye!
Output - Mixing Strings and Numbers
There are two ways to include variables and strings in the same output:
Example:
const q = 5
const price = 3.99
// concatenation
console.log("You bought " + q + " apples for $" + price + " each.")
// interpolation
console.log(`You bought ${q} apples for $${price} each.`)
Variables in Javascript
Example:
let x = 1;
x = 25 // 1. variable can be reassigned
const z = 3;
if (z === 3) {
let y = 200;
console.log(y); // expected output: 200
}
Example:
const x = 1;
x = 99 // 1. ERROR: x is read only!
const z = 10;
if (z > 2) {
const y = 200
console.log(y); // expected output: 200
}
Javascript variables have a data type. The most common data types are:
Use the built in typeof(...) function to view the data type of a Javascript variable
When you declare a variable, Javascript performs type inference to determine the
variable’s data type.
● JavaScript tries to guess the data type based on the initial value you assign
the variable.
JavaScript is a Loosely Typed Language
Strongly typed languages are very strict with variables and their data types.
1. Type Identification:
● You must identify the type of data that will be stored in the variable
However, if you do not assign the variable a value, it’s type is set to to undefined
let x
console.log(x) 0x8A521
let y = null
null 0x2841B
console.log(y)
Output - String Interpolation
Use the ${...} syntax to specify where your variable values should go
Example:
Example:
● Addition: +
● Subtraction: -
● Multiplication: *
● Division: /
● Modulus: %
Example:
console.log(result);
Sometimes, developers need to convert between numbers and string data types:
To convert between strings and numbers, use the following built in functions:
String to Numbers:
const a = "3.5"
Numbers to String
const d = 400
const e = String(d) // produces "400"
const f = d.toString() // produces "400"
Variables: Testing for Equality