Traditionally JavaScript has been used only in the frontend, so the code was loaded to the browser and executed there. But in the late-2000s Node.js was created and that made possible to use it also in the backend.
This is not a JavaScript tutorial, so I will introduce only those things which are the most relevant in order to be able to understand the REST API examples.
To make REST API with JavaScript, you need to learn how to use Node.js and here is one tutorial for that https://www.w3schools.com/nodejs/default.asp.
JavaScript provides three ways to declare variables: var, let, and const. They
differ in terms of scope, reassignment rules, and usage.
if, for).var x = 10;
if (true) {
var x = 20; // Same variable!
console.log(x); // Outputs 20
}
console.log(x); // Outputs 20
let y = 10;
if (true) {
let y = 20; // Different variable!
console.log(y); // Outputs 20
}
console.log(y); // Outputs 10
let.const z = 10;
z = 20; // Error: constants cannot be reassigned!
const arr = [1, 2, 3];
arr.push(4); // Allowed, modifying the contents
console.log(arr); // Outputs [1, 2, 3, 4]
Here is a quick comparison of the three declaration types:
| Feature | var | let | const |
|---|---|---|---|
| Scope | Function scope | Block scope | Block scope |
| Reassignment | ✓ Allowed | ✓ Allowed | ✗ Not allowed |
| Redeclaration | ✓ Allowed | ✗ Not allowed | ✗ Not allowed |
| Hoisting | Yes (initialized as undefined) | Yes (not initialized - temporal dead zone) | Yes (not initialized - temporal dead zone) |
| When to use | Avoid in modern code | When value needs to change | Default choice (use when possible) |
Best Practice: In modern JavaScript, use const by default. Only use let when you know the variable value will need to be reassigned. Avoid using var in new code.
If you know how to handle associative arrays, you can easily handle the data which you will get from the database.
Associative array is an array where the fields have names. Here is an example of that
const bookData=[
{'name' :"C++", 'author' :"Jim Smith"},
{'name' :"Java", 'author' :"Lisa Jones"},
{'name' :"MySQL", 'author' :"Bob Daniels"}
];
You can print all books from the array like this
console.log(bookData);And you can print the first row from the array like this
console.log(bookData[0]);
Here is an example about using objects in JavaScript
//person is an object
const person={
fname:'Teppo',
lname:'Testi',
showData: function(){
console.log(this.fname+ " "+this.lname);
}
};
//calling the showData method of the person object
person.showData();
And another example where the object includes associative array
const person={
personData:[
{'fn' :"Jim", 'ln' :"Smith"},
{'fn' :"Lisa", 'ln' :"Smith"},
{'fn' :"Ann", 'ln' :"Jones"},
],
showData: function(){
console.log(this.personData);
},
insertData: function(addFn, addLn){
this.personData.push({fn:addFn, ln:addLn});
}
};
person.insertData('Teppo','Testi');
person.showData();
A callback is a function that is passed as an argument to another function, which then executes it at an appropriate time. This pattern is not unique to JavaScript - it exists in many programming languages like Python, C++, Java, and others.
Callbacks are used in several scenarios:
Here is a simple example showing how callbacks work with array operations:
const numbers = [1, 2, 3, 4, 5];
// The function 'double' is passed as a callback to 'map'
function double(num) {
return num * 2;
}
const doubled = numbers.map(double);
console.log(doubled); // [2, 4, 6, 8, 10]
In this example, the double function is a callback that gets executed for each element in the array.
Callbacks are especially important for asynchronous operations - tasks that don't complete immediately. Here is an example that demonstrates asynchronous callbacks:
setTimeout(doSomething,2000);
function doSomething(){
console.log("Demonstrating the callbacks");
}
console.log("The application is started");
The setTimeout function takes two parameters: a callback function (doSomething) and a delay
in milliseconds. The callback will be executed after the specified delay.
Because setTimeout is asynchronous, it doesn't block the execution of subsequent code.
The program continues immediately to the next line, which is why "The application is started"
appears in the console before "Demonstrating the callbacks", even though the setTimeout
call appears first in the code.
doSomething is passed as an argument to setTimeoutsetTimeout doesn't block execution - code continues immediatelyAn anonymous function is a function without a name. These are commonly used when you need a function only once, typically as a callback.
Instead of defining a separate named function, you can write the function directly as an argument:
// Anonymous function as callback
setTimeout(function(){
console.log("Demonstrating the callbacks");
}, 2000);
console.log("The application is started");
When to use anonymous functions:
Here are more examples:
// Array operations
const numbers = [1, 2, 3, 4, 5];
const squares = numbers.map(function(n) {
return n * n;
}); // [1, 4, 9, 16, 25]
// Event listener
button.addEventListener('click', function() {
console.log('Button clicked!');
});
Arrow functions (introduced in ES6) provide a shorter syntax for writing functions. They use the
=> syntax and are commonly used for callbacks and short function expressions.
Click the buttons below to see different aspects of arrow functions:
While callbacks are fundamental to JavaScript, they can lead to complex, nested code structures often referred to as "callback hell" when dealing with multiple asynchronous operations. Modern JavaScript offers cleaner alternatives: Promises and async/await.
Click the buttons below to learn about modern alternatives to callbacks:
Here is a short article about callbacks https://www.freecodecamp.org/news/javascript-callback-functions-what-are-callbacks-in-js-and-how-to-use-them/