Allgemeine Snippets
Auf dieser Seite sind verschiedene JavaScript Snippets zu allgemeinen Themen.
Die Snippets stellen Beispiele dar. Passe diese gerne für deine Zwecke an.
Inhaltsverzeichnis
Prüfen, ob Variable null
oder undefined
ist
let myVar = null;
if (myVar === undefined || myVar === null) {
console.log(`Variable is either undefined or null.`);
} else {
console.log(`Variable is defined and not null.`);
}
Variable is either undefined or null.
Prüfen, ob Variable eine Funktion ist
let myFunction = function() {
console.log("Hello world");
};
if (typeof myFunction === "function") {
console.log("Variable is of function type.");
} else {
console.log("Variable is not of function type.");
}
Variable is of function type.
Queue-Struktur in JavaScript
class Queue {
constructor() {
this.items = [];
}
enqueue(element) {
this.items.push(element);
}
dequeue() {
if (this.isEmpty()) {
return "Underflow";
}
return this.items.shift();
}
front() {
if (this.isEmpty()) {
return "Queue is empty";
}
return this.items[0];
}
isEmpty() {
return this.items.length === 0;
}
size() {
return this.items.length;
}
print() {
console.log(this.items);
}
}
const queue = new Queue();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
console.log("Queue elements");
queue.print();
console.log("Front element:", queue.front());
console.log("Dequeued element:", queue.dequeue());
console.log("Queue size:", queue.size());
console.log("Is empty:", queue.isEmpty());
Queue elements
[ 1, 2, 3 ]
Front element: 1
Dequeued element: 1
Queue size: 2
Is empty: false
Prüfen, ob Variable eine Funktion ist
let myFunction = function() {
console.log("Hello world");
};
if (typeof myFunction === "function") {
console.log("Variable is of function type.");
} else {
console.log("Variable is not of function type.");
}
Variable is of function type.
Stack-Struktur in JavaScript
class Stack {
constructor() {
this.items = [];
}
push(element) {
this.items.push(element);
}
pop() {
if (this.isEmpty()) {
return "Underflow";
}
return this.items.pop();
}
peek() {
return this.items[this.items.length - 1];
}
isEmpty() {
return this.items.length === 0;
}
size() {
return this.items.length;
}
print() {
console.log(this.items);
}
}
const stack = new Stack();
stack.push(1);
stack.push(2);
stack.push(3);
console.log("Stack elements");
stack.print();
console.log("Top element:", stack.peek());
console.log("Popped element:", stack.pop());
console.log("Stack size:", stack.size());
console.log("Is empty?", stack.isEmpty());
Stack elements
[ 1, 2, 3 ]
Top element: 3
Popped element: 3
Stack size: 2
Is empty? false
Zwei Variablen vertauschen
let variableOne = "John";
let variableTwo = "Kate";
console.log("Before swapping");
console.log(`variableOne: ${variableOne}`);
console.log(`variableTwo: ${variableTwo}`);
console.log("=====");
// Swapping variables
let temp = variableOne;
variableOne = variableTwo;
variableTwo = temp;
console.log("After swapping");
console.log(`variableOne: ${variableOne}`);
console.log(`variableTwo: ${variableTwo}`);
Before swapping
variableOne: John
variableTwo: Kate
=====
After swapping
variableOne: Kate
variableTwo: John
Parameter in setTimeout übergeben
function myFunction(parameter) {
console.log("Parameter:", parameter);
}
let myParameter = "Hello world";
setTimeout(function() {
myFunction(myParameter);
}, 1000);
Parameter: Hello world
Daten in Konsole eingeben
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
})
readline.question('What is your name? ', name => {
console.log(`Hello ${name}` )
readline.close()
});
What is your name? Max
Hello Max
Umgebungsvariablen setzen
Bei der Ausführung von JS-Dateien in NodeJS kann man Umgebungsvariablen in das Script hineingeben.
const id = process.env.USER_ID
const key = process.env.USER_KEY
console.log(id)
console.log(key)
Verwendung
USER_ID=1 USER_KEY=somekey node common_environment_variables.js
1
somekey
Fehler abfangen in try-catch
Der try-catch
Block funktioniert wie eine Art Auffangschale. Vor allem der catch
Block. Dieser fängt alle, im try
Block, geworfenen Fehler auf und kann sie entsprechend behandeln.
const myArray = [10, 23, 29, 34, 7, 39, 40];
try {
if (myVar < 10) {
throw new Error("My var is false");
}
} catch (error) {
console.log("ERROR IN CATCH:", error);
} finally {
console.log("Done");
}
ERROR IN CATCH: ReferenceError: myVar is not defined