You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1.5 KiB
1.5 KiB
Javascript Fundamentals Review
Primative Data Types
Javascript has several basic data types:
'this is a string' // string (text)
123 // number (int)
1.2 // number (float)
true // boolean
false // boolean
Variables
Declaring variables is done with the keyword var. Variables are dynamically typed (decided during execution) since there is no compilation process.
var foo = 'string';
var bar = 1;
Reassignment is done by omitting the var keyword. Variables are loosely typed (can be reassigned to any other value, regardless of their initial value)
var foo = 'a string';
foo = 123.4;
Functions and Scope
Declare functions with keyword function. Do not need to specify return type.
function myFunc(){
return 5;
}
To execute a function, invoke its name with parentheses
myFunc();
Can pass parameters into functions
function addFive(numberToAddFiveTo){
return numberToAddFiveTo + 5;
}
addFive(6); // returns 11
Variables declared inside a function cannot be accessed outside of it
function foo(){
var myVar = 1;
return myVar;
}
console.log(myVar); // error
but variables declared outside a function are accessible within it
var myVar = 1;
function foo(){
return myVar;
}
console.log(foo());