JavaScript Basic Syntax
JavaScript Basic Syntax
Hello world (Printing & Commenting)
// this is a comment
console.log("Hello world")
/*
* This is a multiline comment
*/
console.log(`This is an variable ${X} in the log `)
Variables
#Block scope
var variable = "I can change and im hoisted" // Variable with hoisting
let myName = "I can change" // Variable with blockscope
const myConst = "I wont change"
Const
It does not define a constant value. It defines a constant reference to a value.
Can NOT:
- Reassign a constant value
- Reassign a constant array
- Reassign a constant object
But you CAN:
- Change the elements of constant array
- Change the properties of constant object
Scope
Global scope
Scope
All scripts and functions on a web page can access it.
let carName = "Volvo";
// code here can use carName
function myFunction() {
// code here can also use carName
}
Block scope
let,const
: Variables declared inside a { } block cannot be accessed from outside the block:
var
Variables declared inside a { } block can be accessed from outside the block.
{
let x = 2;
}
// x can NOT be used here
{
var x = 2;
}
// x CAN be used here
Operators
??
?.
safe call!!
...object
Object spread
Data Types
Number
All numbers in JS are always one type: Double (64-bit)
BigInt
String
Boolean
Undefined
Object
Null
Null vs Undefined
Symbol
Data Structures
Sets
Functions
- similarities to Kotlin Syntax
- assign variables
function myFunction(param,param,...theRestOfParams){
return param * param
}
Anonymous
Hoisting
Control Flow
- if
- ternary
- switch
- for