I'll post latest version of javascript later on after finishing studying it.
What you have to be careful in Javascript
alert("Hello"); // --> Function("message")end(;)
When you type a quotation marks in a word processor for example, MS word or google Docs then you actually get very different symbols from what you use in programming!
There is no space between "alert" and ("Hello") and ";". You can add those spaces and It will also work but If there is no space there, It will make your code much more uniformed and readable!
You can also use single quotation mark but as a style convention, most javascript programmers in this case will use the double quotes.
DataType in Javascript
Text -> "text"
Numbers -> 1, 2, 3,....
Boolean -> true or false
Declare Variable In Javascript
var name = "Junho"; // --> keyword name = value;
How to name variable In Javascript?
- Give your variables meaningful name.
- You can't name the variables using built-in functions or Data types. But you can contain the keywords in variables.
- Variables can't begin with number.
- Variables can't contain any space.
- Variables only contain letters, numbers, $ and underscore.
- You should use Camel case. ex) yourName
Concatenate Strings
console.log("a" + "b"); // == "ab"
console.log("a" + " " + "b"); // == "a b"
String Length
console.log("ab".length); // == 2
Slicing
var li1 = [1, 2, 3, 4];
console.log(l1.slice(0,2)); // == [1, 2]
Calculation
var a = 3 + 5; // add
var b = 3 - 5; // sub
var c = 3 / 5; // divide
var d = 5 % 3; // remainder after division
var e = 5 * 3; // prod
- Precedence : (), (*, /, %), (+,-), (>,<,>=,<=)
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
Condition
console.log(1 == 2) // if 1 is equal to 2 and doesn't care about types
console.log(1 === 2) // if 1 is equal to 2 and cares about types
console.log(1 != 2) // if 1 is not equal to 2
console.log(1 > 2) // if 1 is bigger than 2
console.log(1 < 2) // if 2 is bigger than 1
console.log(1 >= 2) // if 1 is bigger than 2 or same with 2
console.log(1 <= 2) // if 2 is bigger than 1 or same with 1
console.log((2 > 1) && (3 > 2)) // if both conditions are satisfied
console.log((2 > 1) || (3 > 2)) // if at least one condition is satisfied
console.log(!(3 > 2)) // if this condition is true then false, If this conditions is false then true
Prefix Increment & Suffix Increment
var x = 1
x += 1 // x = x + 1
x++ // x = x + 1 increase +1 after returning the variable
++x // increase +1 first
var y = 3
x += y // x = x + y
Function
Allows you to create a series of instructions and package it into a block of code.
function orderStuff1(){
return "item 1"
}
function orderStuff2(number){
return "item" + number
}
orderStuff1();
orderStuff2(5);
Random
var n = Math.random(); // 0 ~ 0.999999
IF-Else
if(track === "clear"){
goStraight();
}else{
turnLeft();
}
- If the condition is false, then the code flows to the else statement and run the function turnLeft() instead.
Javascript Arrays
Stores a collection of related items into the same container or the same variable.
var items = ["item1","item2","item3","item4","item5"]; // declare an array
items[1] // select "item2"(index range is from 0 to length of Array - 1 )
// See if it matches with any of items inside the array. If it deos then true, If it doesn't then false.
items.includes(val)
// You can add an item to Array.
items.push(val)
Javascript Loops
var idx = 0
while(idx < 3){ // While variable idx is smaller than 3
// Do something
}
for(var i = 0;i < 2;i++){ // For variable I is smaller than 2
// Do something
}
While VS For
- While loop: It is good to use When you don't know how many times the iteration will be.
- For loop: it is good to use when you essentially are trying to iterate or You are trying to run a peice of code as many times as you know.