Javascript is one of the most used programming languages and in this guide I will review briefly the basics of the language for someone who just wants to take a look at the language or see if it's the kind of language they want to learn
What is JS?
Javascript is a high-level, weakly typed, multi-paradigm language used for both Front-end (executed on the browser and used along HTML and CSS) and Back-end (on server side) Web Development and standardized by the EcmaScript (ES).
JS Basics
To print something to the console, the console object must be used together with the log method and the value to be printed in parentheses.
The data types in Javascript are:
- string (text strings or simple characters, each one of them having a specific index)
- Numeric (integers, bigInt for large numbers and float for decimals)
- Boolean (true or false)
- undefined
- null
- objects
- symbols
They can be declared with the word var (depreciated since ES6) or with let in the case of variables that can be changed throughout the program or const in the case of constant ones. At the end of each statement, though not necessary for the program, a semicolon (;) is used.
Javascript Operators:
- > more than < less than
- >= more or equal than <= less or equal than
- == not strict equal (5 == '5' true)
- === strict equal (5 === 5 true) / (5 === '5' false)
- != not strict different
- !=== strict different
console.log('Hello World');
var example = null;
let example2; // undefined
example2 = true;
const number = 5;
number = 10; // Error: Assignment to constant variable
Functions
Functions in Javascript allow you to execute pieces of code at any time without having to write them again. Normal functions are declared with function functionName (parameters) { function content }. They can also be abbreviated and wrapped in variables as in the case of anonymous functions. To call a function, nameOfTheFunction(parameters) is used, and in case the latter do not exist, each one can be given a default value from the function through the equals symbol (num1 = 1, num2 = 2) . To encapsulate and return the value of a function, the return keyword is used.
// Common functions
function sum (num1, num2) {
return num1 + num2;
}
// "Arrow head" Functions
const sum1 = num1 => {
return num1 + 1;
}
const multiply = (num1, num2) => num1 * num2 ; /* Implicit return keyword, only available on this type of functions */
Loops
Pieces of code that can be repeated over and over again until a given condition is met. They are used both to traverse strings, arrays and other values. They can be for loops and they can also be used as a while loop, which runs as long as a condition (specified outside of the loop) is met and do while loops (do a yes-or-yes action first and then set the condition to be false, if not, continue). However while loops are used less because of the increased change for infinite loops.
const firstVariable = 'Hello'
for (let i = 0; let < 5; let++) {
/* Loop index is 0 and 1 is added at each iteration
(loop content execution) till i stops being less than 6 */
console.log(firstVariable[i]) // prints the letter where the loop is iterating
// Prints h e l l o in separated lines
}
const secondVariable = 'World!'
// While loops example
let j = 0;
while (j < 6) {
console.log(secondVariable[j]);
j++;
}
let k = 0;
do {
k++;
console.log('Increasing k');
} while (k < 3)
This has been my first post, hope you like it, I would be glad if you put any suggestions in the comments.
Top comments (3)
This was a pretty nice article although i think there was a mistake in your coding.
You declared a variable 'i' with the value of 0 but you were now using the keyword to run the loop your code should prolly return an error in ur console log.
Thank you for replying, I am using the i variable which will first be 0, then 1, etc as the index number of the string so that the code will be
console.log(firstVariable[0])
console.log(firstVariable[1])
But I also found that there was an error in my code, i should have used i < 5 because in the last iteration it prints undefined😅
before published, firstVariable was filled with buenas;-)