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)