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)

An Animated Guide to Node.js Event Loop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.