DEV Community

vitor-saldanha
vitor-saldanha

Posted on

JavaScript Introduction Concepts 📍️

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)