<script>
//should be in your "head" tag
console.log("Hello world")
//in the console, the message "Hello world" will be printed out
</script>
<script src = "script.js">
</script>
<script>
// this is a comment
/*
this is also a comment
*/
//you declared variable 'a' and its value is 10
let a = 10;
// this is what a javascript function looks like
let function_a = (param)=>{
let result = param * 2
return result
}
</script>
// this is what it looks like in your JS file or tag
//please go to your developer console or inspect element to see it in action!
console.log(output)
// this is what it looks like in your JS file or tag
alert(output)
let var_1 = "Hello world"
// the value of variable 'var_1' is "Hello world"
To check the data type of a variable, type the following:
typeof var_1
//this returns the data type of given variable
//it returns "string" in this case
let a = 1
let b = a++
value of b is 1
let c = 1
let d = c--
value of d is 0
let funct_name = (param1)=>{
output = param1 ** 2
return output
//output is the square of the value of variable 'param1'
}
You can try it here:
// format is: for(i = startingValue, i < endingValue ; i ++ )
// ending value can be less than i,a nd i ++ should be replaced with i -- if so.
for(let i = 0, value,i++){
//meant to 'print out the number'
console.log(i)
}
You can try it here:
// format is: while( condition )
// ending value can be less than i,a nd i ++ should be replaced with i -- if so.
while(i > value){
//meant to 'print out the number'
i ++
}
You can try it here:
// > means greater than
// < means less than
// === means equal to
// || means 'or':
// eg: (a< 10) || (a>20) means a has to be less than 10 or greater than 20
// && means 'and':
// eg: (a< 10) && (b>20) means a has to be less than 10 and b has to be greater than 20
if(value < 10){
console.log("too low")
//first condition
}
else if(value < 20){
console.log("still alright")
//second condition (if conditions on top fails)
}
else{
// if all else fails!
console.log("excellent")
}
You can try it here:
let str = "test"
let string_length = str.length
//string_length = 4
let stringg = "omg"
stringg.includes("o")
//returns true
string.includes("gg")
//returns false
let first = "Hello"
let second = "world"
console.log(first.concat(second))
//output is "hello world"
let string = "O M G"
console.log(string.split(" "))
//output:O,M,G
//returns ["O","M","G"]
let string = "I Love Javascript"
let lowered = string.toLowerCase()
//lowered = i love javascript
let uppered = string.toUpperCase()
//uppered = I LOVE JAVASCRIPT
let num = 12
num_1 = num.toString()
//num_1 = "12"
let num = 5.6789
console.log(num.toFixed(2))
//displays 5.68
console.log(num.toFixed())
//displays 6
Math.round(5.66) //this will return a 6
Math.ceil(5.11) //this will return a 6
Math.min(3,5,6,8,2,1,9)
// returns 1
Math.max(3,5,6,8,2,1,9)
// returns 9
Math.trunc(7.99)
//returns 7
//the format is Math.floor(Math.random()*higher_number) + lower_number
console.log(Math.floor(Math.random()*11))
//returns a random number from 0 to 11
console.log(Math.floor(Math.random()*111)) +2
//returns a random number from 2 to 111
let arr = []
//arr = []
let arr = new Array("a","b","c")
//arr = ["a","b","c"]
let arr = [3,2,6,4]
arr.sort()
//now arr = [2,3,4,6]
let arr = ["abc","bcd","cde"]
arr = arr.toString()
// now arr = "abc,bcd,cde"
let arr = ["a","b","c"]
arr.forEach((item)=> console.log(item))
//displays each item in the console
let arr = [1,2,3]
arr.push(4)
//arr = [1,2,3,4]
let target = 10
let arr = [2,4,6,8,10,12,14]
arr = arr.filter((item)=> item !== 10)
//arr = [2,4,6,8,12,14]
const new_object = {
//u can think of attribute as variable
attribute_1:value_1,
attribute_2:value_2,
attribute_3:value_3,
// you can think of method as functions, but usable only by occurrences of the object
method_1:function(){
//whatever u want it to do
},
method_2:function(){
//whatever u want it to do
},
}
//to initialise an instance of object
let new_instance = new_object()
//to access an object's attribute
new_instance.attribute_1
//this will return whatever the value of "attribute_1" of the instances is
//this will make the instance call the function method_1
//method_1 will run whatever that is inside it
new_instance.method_1()
//selects element(s) with ID of "haha"
let get_by_id = document.getElementById("haha")
//selects elements by tag name; all anchor tags are chosen
let get_by_id = document.getElementByTagName("a")
//selects elements by class name; all element(s) with class "card" is/are chosen
let get_by_id = document.getElementsByClassName("card")
//selects elements by css selectors; all element(s) with CSS selector "a .item" are chosen
let get_by_id = document.querySelectorAll("a .item")
<!-- In your html file: -->
<!-- In your element: event = function-->
<!-- When this button is clicked, the function "clickMe()" is triggered -->
<button onclick = "clickMe()"> Click Me! </button>
//in your JS file:
//a common format is: target.addEventListener(event,function)
let target = document.getElementById("haha")
target.addEventListener("click",clickMe)
//here is an example of an ajax request:
let request = new XMLHttpRequest();
request.onreadystatechange = function(){
if(this.readyState == 4 && this.status ==200){
//means if responder is ready
//this.responseText is the response data in string form
console.log(this.responseText)
let url = "//this be a random url"
//request.open(request_type,url,is_asynchronous)
request.open("GET",url, true) //specify the type of request
request.send()
}
}
let item = {key_1:value_1,
key_2:value_2}
These are the ways which we access JSON object properties:
item[key_1] // accesses value_1
item.key_1 //accesses value_1
JSON parse and Stringify
This is how we parse and stringify json objects:
let a = {item:"a",no:"1",
item:"b",no:"2",
item:"c",no:"3"
}
//NOTE: both only works if the string/object are in JSON format
let stringed_JSON = JSON.stringify(a) //converts JSON to string
let parsed =JSON.parse(stringed_JSON) //converts string to JSON