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:
-
var
is used in pre-ES6 version of JavaScript -
let
declares a variable that the content can change. -
const
declares 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)