Sitemap
Nguyễn Tấn Phát

Nguyễn Tấn Phát

Basic JavaScript

5 min readMar 8, 2022

--

Press enter or click to view image in full size
Google.com

What is JavaScript?

Javascript (know as JS) is a popular language for creating modern websites and web applications today. JS application is not only on the client side (on the client browser) but also on the server side.

Currently, for those of you who are following the frontend, you can use JS to make website applications with the support of many frameworks and libraries such as Reactjs, Angular, Vue… , that were written by JS and are very popular.

Each library and framework has its own advantages and disadvantages, but depending on the size and nature of the project’s requirements or simply the most suitable for the team, it will be chosen to develop your website or web application. . But these libraries all have one thing in common: they are written in JS language.

  1. How to set up?

We need 2 files: the index.html file with the app.js file attached as follows below

<!DOCTYPE html>
<html>
<head>
<meta charset = "UTF-8" />
<meta name="viewport" content="width=device-width, initial- scale=1.0">
<title>Learn Basic JavaScript</title>
</head>

<body>

<script src="app.js"></script>

</body>
</html>

2. Variables (var, let, const) hoisting, scope

In JS uses variables, you imagine variables like small boxes to store things. To use a variable, we must declare it with the var, let or const keyword. After the keyword will be the variable name, after the declaration is completed, you can use the assignment with equal sign ‘=’ in JS to declare the value for that variable.

The rule for naming variables is that variable names must start with a letter or the underscore character ‘_’. Must not start with a number
Variable names can be named according to camelCase, Pascal, or underscore rules.

Example:

var firstName = ‘Phat’ // camelCase-rule naming
let Last Name = ‘Nguyen’ // Pascal Rule
const middle_name = ‘Love JS’// Underscore Rule
  • Hoisting:

Var

When we declare a variable (and a function too) with the var keyword, JS will defaults to putting that variable at the top of the scope where that variable can be called. At that time, the value of the variable is always returned as undefined even though we have assigned a value to that variable when we declared the variable. For example when this line of code

console.log(message)
var message = "hoisting"

Then with the hoisting , JS code will run in the following order:

var message // this is hoisting Js brings this variable to the top of the variable callable scope
console.log(message) // undefined
var message = "hoisting"

In fact, in normal programming, that programming language must report errors here so that the programmer can correct the code in time. As for Js, it doesn’t report errors but still gives undefined results, how can I find errors in thousands of lines of code.

Why this hoisting mode? As I guess, it may be because JS allows to initialize variables without using the var keyword. Example

message = "Initialize variable without var . keyword"
console.log(message)
// The return is
`Initialize variable without var . keyword`

So how can we fix this? Fortunately, JS also provides a workaround using usestrict. For example

'use strict'
console.log(message)
message = "hoisting"
//Error: 'Message is not defined'.

Let, const

Get Nguyễn Tấn Phát’s stories in your inbox

Join Medium for free to get updates from this writer.

These are variable declarations starting with later improved versions of JS , let and const are not hoisting. The main difference between them is that let can be reassigned and const cannot be reassigned to the entire variable.

let web = 'phatnguyen.com'
console.log(web) // phatnguyen.com
web = 'depzai.com'
console.log(web) // web variable is reassigned value depzai.com
// With const
const site = 'phatnguyen.com'
console.log(site)
site = 'depzai.com' // Error message cannot reassign value

But with const, we can change the internal value of the variable to be object or array.

const dog = {name:'Dog'}
dog.name = 'Husky'
console.log(dog) // {name: 'Husky'} --> which means the value inside the object of the const variable can be changed.

const dog= ['Husky', 'Lazy']
dog.push('And love his girlfriend')
console.log(dog) // ['Husky', 'Lazy', 'And love his girlfriend']

Scope

  • Global Scope

Which is the variable can be used outside or inside a function. For example

var dog = 'Husky'

// We can use the above variable here
function eat = {
// We can also use variables here
}
  • Block Scope

Block here is a pair of curly braces {}, follow bellow example

{
var dog = "Husky"
}
// the dog variable is still callable here outside the block scope braces

In ES6, we can use:

{
let dog = "Husky",
const feel = 'happy'
}

3. Data type

  • Number: 1, 2, 3, …
  • String: ‘phat dep zai’
  • Boolean: true, false
  • Null
  • Underfined
  • Symbol(ES6)
  • object

Declare

  • Constructor: is a way to declare primitive data types
  • Wrapper object: is a way to declare and create objects for boolean, number, string with wrapper objects respectively Boolean, Number, String. For example:
let str = new String ('hello')
console.log (typeof str) // object
console.log(str) // String {"hello"}

Reference, Rereference

  • Reference
// 1. Compare by value
'phat' === 'phat' // true
1 === 1 // true
// 2.
Always Immutable
var year = 2022
// When we add the name attribute, it won't work
year.name = 'good year'
console.log(year.name) // undefined
  • Rereference
// Compare by reference
{} === {} // false
var name1 = ["phat"]
var name2= ["phat"]
name1 === name2 // false
//
mutable can be changed
var web1 = {name:'phat'}
var web2 = web1
web2.name = 'dep zai'
console.log(web1)

4. Operators

There are about 10 different types of operators in Js such as

  • Assignment operators
let a = 1
a += 2 // 3
a -= 2 // 1
a *= 2 // 2
a **= 2 // 4
a /= 2 // 2
a %= 2 // 0
  • Comparison operators
console.log('2' == 2) // true
  • Arithmetic operators
1 + 2 // plus
2 - 1 // substract
3 * 2 // multi
2 ** 4 // 2 exponential 4 = 16
4 / 2 // divide
10 % 3 // the result is 1 which means divide by remainder
i++ // increment the value by 1 and return the new value
++i // increments the value by 1 and returns the original value before incrementing
// Example
a=0, b=0;
a++; // a = 1
++b; // b = 1
b = ++a; // b = a = 2
a = b++; // a = 2, b = 3
//
i-- // reduce the value by 1 and return the new value
--i // reduces the value by 1 and returns the original value before incrementing
  • Bitwise operators
  • Logical operators
  • String operators
  • Conditional (terary) operator
let point = 5
let result = point >= 5 ? "Learn continue" : "Learn again"
console.log(result) // Learn continue
  • Comma operator
  • Unary operators
  • Relation operators

This post is so long, I will continue in part 2. I will update link part 2 soon 😉

“Thanks for reading. I hope it helped you. See you in the next post. “

“Happy coding!!!”.

Reference

--

--