Skip to content

Instantly share code, notes, and snippets.

@pascalmaddin
Created January 4, 2021 20:43
Show Gist options
  • Save pascalmaddin/f36e0419ac0fb331bca552d47b379f14 to your computer and use it in GitHub Desktop.
Save pascalmaddin/f36e0419ac0fb331bca552d47b379f14 to your computer and use it in GitHub Desktop.
basics.js
let Variable;
const Variable2;
var Variable;
/*
A semicolon at the end of a line indicates where a statement ends.
A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($). Subsequent characters can also be digits (0–9).
Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) as well as "a" through "z" (lowercase).
*/
// After declaring a variable, you can give it a value:
myVariable = 'Bob';
// Also, you can do both these operations on the same line:
let myVariable = 'Bob';
// You retrieve the value by calling the variable name:
myVariable;
// After assigning a value to a variable, you can change it later in the code:
let myVariable = 'Bob';
myVariable = 'Steve';
// string
let myVariable = 'Bob';
// number
let myVariable = 10;
// boolean
let myVariable = true;
// array
let myVariable = [1,'Bob','Steve',10];
//Refer to each member of the array like this:
//myVariable[0], myVariable[1], etc.
// object
// This can be anything. Everything in JavaScript is an object and can be stored in a variable.
// --> Operatoren
/* Addition
6 + 9;
'Hello ' + 'world!';
*/
/*
Subtraction, Multiplication, Division
9 - 3;
8 * 2; // multiply in JS is an asterisk
9 / 3;
*/
/*
Assignment
let myVariable = 'Bob';
*/
/*
Equality
let myVariable = 3;
myVariable === 4;
*/
// --> Kontrollstrukturen
let iceCream = 'chocolate';
if(iceCream === 'chocolate') {
alert('Yay, I love chocolate ice cream!');
} else {
alert('Awwww, but chocolate is my favorite...');
}
"The fee is " + (isMember ? "$2.00" : "$10.00")
var elvisLives = Math.PI > 4 ? "Yep" : "Nope";
var firstCheck = false,
secondCheck = false,
access = firstCheck ? "Access denied" : secondCheck ? "Access denied" : "Access granted";
console.log(access); // logt "Access granted"
// --> Funktionen
/* A function in JavaScript is similar to a procedure—a set of statements that performs a task or calculates a value,
but for a procedure to qualify as a function, it should take some input and return an output where there is some obvious
relationship between the input and the output. To use a function, you must define it somewhere in the scope from which you wish to call it. */
function square(number) {
alert(number);
}
square(2);
function square(number) {
return number * number;
}
var test = square(2);
alert(test);
const square = function(number) { return number * number }
var x = square(4) // x gets the value 16
const factorial = function fac(n) { return n < 2 ? 1 : n * fac(n - 1) }
console.log(factorial(3))
// A function can call itself. For example, here is a function that computes factorials recursively:
function factorial(n) {
if ((n === 0) || (n === 1))
return 1;
else
return (n * factorial(n - 1));
}
// The following variables are defined in the global scope
var num1 = 20,
num2 = 3,
name = 'Chamahk';
// This function is defined in the global scope
function multiply() {
return num1 * num2;
}
multiply(); // Returns 60
// A nested function example
function getScore() {
var num1 = 2,
num2 = 3;
function add() {
return name + ' scored ' + (num1 + num2);
}
return add();
}
getScore(); // Returns "Chamahk scored 5"
//The following example shows nested functions:
function addSquares(a, b) {
function square(x) {
return x * x;
}
return square(a) + square(b);
}
a = addSquares(2, 3); // returns 13
b = addSquares(3, 4); // returns 25
c = addSquares(4, 5); // returns 41
// --> SCHLEIFEN
// for statement
for (let step = 0; step < 5; step++) {
// Runs 5 times, with values of step 0 through 4.
console.log('Walking east one step');
}
// do while statement
//In the following example, the do loop iterates at least once and reiterates until i is no longer less than 5.
let i = 0;
do {
i += 1;
console.log(i);
} while (i < 5);
// while statement
//The following while loop iterates as long as n is less than 3:
let n = 0;
let x = 0;
while (n < 3) {
n++;
x += n;
}
// Infinite loops are bad!
while (true) {
console.log('Hello, world!');
}
// break statement
for (let i = 0; i < a.length; i++) {
if (a[i] === theValue) {
break;
}
}
// for statement
const arr = [3, 5, 7];
for (let i of arr) {
console.log(i); // logs 3, 5, 7
}
// DAS BRINGT UNS ZU ARRAYS
// create array
let fruits = ['Apple', 'Banana']
console.log(fruits.length) // 2
// Access an Array item using the index position
let first = fruits[0]
// Apple
let last = fruits[fruits.length - 1]
// Banana
// Loop over an Array
fruits.forEach(function(item, index, array) {
console.log(item, index)
})
// Apple 0
// Banana 1
// Add an item to the end of an Array
let newLength = fruits.push('Orange')
// ["Apple", "Banana", "Orange"]
// Remove an item from the end of an Array
let last = fruits.pop() // remove Orange (from the end)
// ["Apple", "Banana"]
// Remove an item from the beginning of an Array
let first = fruits.shift() // remove Apple from the front
// ["Banana"]
// Add an item to the beginning of an Array
let newLength = fruits.unshift('Strawberry') // add to the front
// ["Strawberry", "Banana"]
// Find the index of an item in the Array
fruits.push('Mango')
// ["Strawberry", "Banana", "Mango"]
let pos = fruits.indexOf('Banana')
// 1
// Remove an item by index position
let removedItem = fruits.splice(pos, 1) // this is how to remove an item
// ["Strawberry", "Mango"]
//Copy an Array
let shallowCopy = fruits.slice() // this is how to make a copy
// ["Strawberry", "Mango"]
// JavaScript arrays are zero-indexed. The first element of an array is at index 0, and the last element is at the index value equal to the value of the array's length property minus 1.
// Using an invalid index number returns undefined.
let arr = ['this is the first element', 'this is the second element', 'this is the last element']
console.log(arr[0]) // logs 'this is the first element'
console.log(arr[1]) // logs 'this is the second element'
console.log(arr[arr.length - 1]) // logs 'this is the last element'
// NUN OBJEKTE
// JavaScript objects are containers for named values called properties or methods.
// You have already learned that JavaScript variables are containers for data values.
var car = "Fiat";
// Objects are variables too. But objects can contain many values. The values are written as name:value pairs (name and value separated by a colon).
var car = {type:"Fiat", model:"500", color:"white"};
// The name:values pairs in JavaScript objects are called properties:
var person = {
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};
// Access
objectName.propertyName oder objectName["propertyName"]
also
person.lastName; oder person["lastName"];
/*
Objects can also have methods.
Methods are actions that can be performed on objects.
Methods are stored in properties as function definitions.
*/
var person = {
firstName: "John",
lastName : "Doe",
id : 5566,
fullName : function() {
return this.firstName + " " + this.lastName;
}
};
var name = person.fullName();
/*
In a function definition, this refers to the "owner" of the function.
In the example above, this is the person object that "owns" the fullName function.
In other words, this.firstName means the firstName property of this object.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment