Home JavaScript
Post
Cancel

JavaScript

1
console.log("Hello World!");

’ ‘ can be used instead of “ “ and ; is optional

Datatypes

1
2
3
var myNum = 5; //global variable
let myString = "Hello"; //local variable
const myBool = true; //local constant
1
2
var myNumbrs[] = {1,2,3}; //array
var dict = {name:"John", age:30, city:"New York"}; //dictionary

Operators

Arithmetic

OperatorNameExample
+Additionx + y
-Subtractionx - y
*Multiplicationx * y
**Exponentiationx ** y
/Divisionx / y
%Modulusx % y
++Incrementi++
Decrementi--

Assignment

OperatorExampleSame As
=x = 5x + y
+=x += 5x = x + 5
-=x -= 5x = x - 5
*=x *= 5x = x * 5
**=x **= 5x = x ** 5
/=x /= 5x = x / 5
%=x %= 5x = x % 5

Comparison

OperatorNameExample
==equal to (same value)x == y
===equal to (same value and type)x === y
!=not equal tox != y
>greater thanx > y
<less thanx < y
>=greater than or equal tox >= y
<=less than or equal tox <= y

Logical

OperatorNameDescriptionExample
&&ANDreturns true if both statements are truex < 5 && x < 10
||ORreturns true if one of the statements is truex < 5 || x < 4
!NOTreverse the result, returns false if the result is true!(x < 5 && x < 10)

Casting

change the data type of a variable

1
2
3
var x = 3
var x_as_string = String(x)
var x_as_int = Number(x_as_string)

String Operations

1
2
3
var a = "Hello, World!"
var b = 420
a += b //Hello, World!420
1
2
3
4
5
6
7
a.length //16
a.slice(0, 5) //returns the first 5 letters (Hello)
a.charAt(1) //returns the second letter (e)
a.toUpperCase() //sets all letters as upper case
a.toLowerCase() //sets all letters as lower case
a.trim() //removes all whitespaces
a.replace("H", "J") //replase a letter

Arrays

can contain different data types

1
2
3
4
5
6
7
var cars = ["Saab", "Volvo", "BMW"];
cars[0] = "Opel"; //change value
cars.push("Audi"); //add value
cars.pop(); //remove last value
cars.shift(); //remove first value
cars.length; //returns the length
cars.sort((a, b) => a - b); //sorts the array

If you just use the sort() function, the array will be sorted alphabetically.

Flow Control

if else statement

1
2
3
4
5
6
7
if (time < 10) {
  greeting = "Good morning";
} else if (time < 20) {
  greeting = "Good day";
} else {
  greeting = "Good evening";
}

switch statement

works with int and char

1
2
3
4
5
6
7
8
9
10
11
let x = "0";
switch (x) {
  case 0:
    text = "Off";
    break;
  case 1:
    text = "On";
    break;
  default:
    text = "No value found";
}

if there is no ‘break’ the next case will be executed too

for loop

1
2
3
for (var i = 0; i < 5; i++) {
  console.log("helloworld"); //prints helloworld 5 times
}

loop through array

1
2
3
for (var i = 0; i < cars.length; i++) {
  console.log(cars[i]);
}

forEach

1
2
3
cars.forEach((item, index, array) => {
  console.log(item, index);
})

functions

1
2
3
4
5
function myFunction(a, b) {
  return a * b;             // Function returns the product of a and b
}

let x = myFunction(4, 3);   // Function is called, return value will end up in x

arrow function

1
2
3
var hello = () => {
  return "Hello World!";
}

Query selectors

select DOM elements

selects the first element with the tag body

1
var body = document.querySelector('body');

selects the element with the id button

1
var button = document.querySelector('#button');

selects all elements with the class someclass and returns an array

1
var classElements = document.querySelectorAll('.someclass');

Create elements

1
2
3
4
var newElement = document.createElement('div'); //creates a new div element
var text = document.createTextNode('Hello World!'); //creates a text node
newElement.appendChild(text); //adds the text node to the div element
document.querySelector('body').appendChild(newElewment); //adds the div element to the body

inner HTML

1
2
var div = document.querySelectorAll('div')[0];
div.innerHTML = '<p>some text</p>'; //replaces the content of the div with a paragraph

HTML attributes

change HTML attributes (attribute names are written in camelCase)

1
2
3
4
5
6
7
var img = document.querySelectorAll('img')[0]; //selects the first image
body.class += 'someclass'; //adds the class someclass to existing classes
img.src = 'path/to/image.jpg';
img.alt = '#';

var a = document.querySelectorAll('a')[0];
a.href = 'https://github.com/SaracenRhue';

CSS attributes

change CSS attributes (attribute names are written in camelCase)

1
2
var body = document.querySelector('body');
body.style.backgroundColor = 'red';

JSON fetch

get data from a JSON file json files are formatted lika a dictionary {} or multiple dictionaries in an array [{},{}]

1
2
3
4
5
6
7
fetch("./src/data.json").then((response) => {
      return response.json();
    }).then((json) => {
      json.forEach((object) => {
        console.log(object.name);
      });
    });
This post is licensed under CC BY 4.0 by the author.