Comments
The first thing to learn is really how to write non-code text (Crazy right?). Comments won't be interpreted, so they are a great way to document your code.
- Single line comments:
// text
//This is a comment
- Multiline comments:
/* text */
/*
this is
also a
comment
*/
Variables
Keywords
There are 3 main ways to declare a variable: var, let and const. The difference is that:
-
varis used in pre-ES6 version of JavaScript -
letdeclares a variable that the content can change. -
constdeclares a variable that the content can't and shouldn't change.
var name = 'Vitor ๐โโ๏ธ๏ธ'
let country = 'Brazil ๐ง๐ท๏ธ'
const hobby = 'Music ๐ต๏ธ'
console.log
If you want to see these variables in the console, use console.log.
console.log(name, country, hobby)
//Prints: Vitor ๐โโ๏ธ๏ธ Brazil ๐ง๐ท๏ธ Music ๐ต๏ธ
Reassign
When reassigning you don't need the keywords. But if you try to reassign const variables, it will throw an error.
country = 'Japan ๐ฏ๐ต๏ธ' //Ok
hobby = 'Eating ๐ฅจ๏ธ' //error: TypeError: invalid assignment to const 'hobby'
Types of Data
Strings
They can be indicated by single or double quotes.
const stringIsText = 'I like dancing ๐๏ธ'
const thisIsAlsoString = "I also like cooking ๐ณ๏ธ"
Numbers
const myFavoriteNumber = 12
Float
const myBudget = 25.2
Boolean
const loveMusic = true
Array
const myFavoriteMeals = ['Shrimp ๐ค๏ธ','Cake ๐๏ธ','Potato ๐ฅ๏ธ']
Objects
const seriesRating = {
title: ['Dark โฐ๏ธ', 'La Casa de Papel ๐ฆ๏ธ', 'The Umbrella Academy โ๏ธ'],
score: [10, 9 ,8]
}
Mathematical Operations
Symbols
// โ๏ธ addition
5 + 5
// โ๏ธ subtraction
5 - 2
// โ๏ธ multiplication
5 * 5
// โ๏ธ division
5 + 2
// โ๏ธ modulo
5 % 2
Assignment Operators
These operators are used to perform an operation and assign at the same time. To save some typing, instead of writing x = x + 2 you can write x+= 2.
+= addition assignment
-= subtraction assignment
*= multiplication assignment
/= division assignment
let moneyEarned = 20
moneyEarned *= 3 //Same as: timePraticed = timePraticed * 3
console.log(timePraticed) //Prints: 60
I really appreciate if you share what you thought and maybe something you struggle so I can cover in the following posts ๐ค๏ธ
Top comments (0)