You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

608 lines
19 KiB

5 years ago
<div align="center">
<h1> 30 Days Of JavaScript</h1>
<a class="header-badge" target="_blank" href="https://www.linkedin.com/in/asabeneh/">
<img src="https://img.shields.io/badge/style--5eba00.svg?label=LinkedIn&logo=linkedin&style=social">
</a>
<a class="header-badge" target="_blank" href="https://twitter.com/Asabeneh">
<img alt="Twitter Follow" src="https://img.shields.io/twitter/follow/asabeneh?style=social">
</a>
5 years ago
<sub>Author:
<a href="https://www.linkedin.com/in/asabeneh/" target="_blank">Asabeneh Yetayeh</a><br>
5 years ago
<small> January, 2020</small>
</sub>
</div>
5 years ago
[Day 2 >>](https://github.com/Asabeneh/30DaysOfJavaScript/blob/master/02_Day/02_day_data_types.md)
5 years ago
![Thirty Days Of JavaScript](./images/day_1_1.png)
- [📔Day 1](#%f0%9f%93%94day-1)
5 years ago
- [Introduction](#introduction)
- [Requirements](#requirements)
- [Setup](#setup)
- [Install Node.js](#install-nodejs)
- [Browser](#browser)
- [Installing Google Chrome](#installing-google-chrome)
- [Opening Google Chrome Console](#opening-google-chrome-console)
- [Writing Code on browser Console](#writing-code-on-browser-console)
- [Console.log](#consolelog)
- [Console.log with multiple arguments](#consolelog-with-multiple-arguments)
- [Comment](#comment)
- [Syntax](#syntax)
5 years ago
- [Arithmetics](#arithmetics)
5 years ago
- [Code Editor](#code-editor)
- [Installing Visual Studio Code](#installing-visual-studio-code)
- [How to use visual studio code](#how-to-use-visual-studio-code)
- [Adding JavaScript to a web page](#adding-javascript-to-a-web-page)
- [Inline Script](#inline-script)
- [Internal script](#internal-script)
- [External script](#external-script)
- [Multiple External scripts](#multiple-external-scripts)
- [Introduction to Data types](#introduction-to-data-types)
- [Number](#number)
- [String](#string)
- [Booleans](#booleans)
- [Undefined](#undefined)
- [Null](#null)
5 years ago
- [Checking Data types](#checking-data-types)
5 years ago
- [Comments](#comments)
- [Variables](#variables)
5 years ago
- [💻 Day 1: Exercises](#%f0%9f%92%bb-day-1-exercises)
5 years ago
# 📔Day 1
5 years ago
5 years ago
## Introduction
5 years ago
5 years ago
**Congratulations** for deciding to participate in a 30 days of JavaScript programming challenge . In this challenge you will learn everything you need to be a JavaScript programmer and in general the whole concepts of programming. In the end of the challenge you will get a 30DaysOfJavaScript programming challenge certificate. Join the [telegram group](https://t.me/ThirtyDaysOfJavaScript).
5 years ago
5 years ago
**A 30DaysOfJavaScript** challenge is a guide for both beginners and advanced JavaScript developers. Welcome to JavaScript. I enjoy using and teaching JavaScript and I hope you will do so. JavaScript is the language of the browser.
5 years ago
In this step by step tutorial, you will learn JavaScript, the most popular programming language in the history of mankind.
You use JavaScript **_to add interactivity to websites, to develop mobile apps, desktop applications, games_** and nowadays JavaScript can be used for **_machine learning_** and **_AI_**.
**_JavaScript (JS)_** has increased in popularity in recent years and has been the leading
programming language for four consecutive years and is the most used programming language on
Github.
5 years ago
5 years ago
## Requirements
5 years ago
5 years ago
No prior knowledge of programming is required to follow this challenge. You need only:
5 years ago
5 years ago
1. Motivation
2. Computer
3. Internet
4. Browser
5. Code Editor
5 years ago
## Setup
5 years ago
I believe you have the motivation and a strong desire to be a developer, computer and Internet. If you have those, then you have everything.
5 years ago
5 years ago
### Install Node.js
5 years ago
5 years ago
You may not need it right now but you may need it for later. Install [node.js](https://nodejs.org/en/).
![Node download](images/download_node.png)
After downloading double click and install
![Install node](images/install_node.png)
5 years ago
We can check if node is installed in our local machine by opening our device terminal or command prompt.
5 years ago
```sh
asabeneh $ node -v
v12.14.0
```
5 years ago
I am using node version 12.14.0, which is the recommended version of node.
5 years ago
5 years ago
### Browser
5 years ago
There are many browsers out there. However, I strongly recommend Google Chrome.
5 years ago
5 years ago
#### Installing Google Chrome
5 years ago
Install [google chrome](https://www.google.com/chrome/) if you do not have one yet. We can write small JavaScript code on the browser console, but we do not use the browser console to develop applications.
5 years ago
![Google Chrome](images/google_chrome.png)
5 years ago
#### Opening Google Chrome Console
5 years ago
You can open Google Chrome either by clicking three dots at the top right corner of the Chrome browser or using a shortcut. I prefer using shortcuts.
5 years ago
5 years ago
![Opening chrome](images/opening_developer_tool.png)
5 years ago
To open the Chrome console using a short cut.
5 years ago
```sh
Mac
Command+Option+I
Windows:
Ctl+Shift+I
```
5 years ago
5 years ago
![Opening console](images/opening_chrome_console_shortcut.png)
After you open the Google Chrome console, try to explore the marked buttons. We will spend most of the time on the Console part. The Console is the place where your JavaScript code goes. The Google Console V8 engine changes your JavaScript code to machine code.
Let us write a JavaScript code on the Google Chrome console:
5 years ago
![write code on console](./images/js_code_on_chrome_console.png)
5 years ago
#### Writing Code on browser Console
5 years ago
We can write any JavaScript code on the Google console or any browser console. However, for this challenge, we only focus on Google Chrome console. Open the console using:
5 years ago
5 years ago
```sh
Mac
Command+Option+I
Windows:
Ctl+Shift+I
```
5 years ago
5 years ago
##### Console.log
5 years ago
To write our first JavaScript code, we used a builtin function **console.log()**. We passed an argument as input data, and the function displays the output. We passed 'Hello, World' as input data or argument in the console.log() function.
5 years ago
5 years ago
```js
console.log('Hello, World!')
```
5 years ago
5 years ago
##### Console.log with multiple arguments
5 years ago
5 years ago
The console.log(param1, param2, param3), can take multiple arguments.
5 years ago
![console log multiple arguments](./images/console_log_multipl_arguments.png)
```js
console.log('Hello', 'World', '!')
console.log('HAPPY', 'NEW', 'YEAR', 2020)
console.log('Welcome', 'to', 30, 'Days', 'Of', 'JavaScript')
```
5 years ago
5 years ago
As you can see from the above snippet code, *console.log()* can take multiple arguments.
5 years ago
Congratulations! You wrote your first JavaScript code using *console.log()*.
5 years ago
##### Comment
5 years ago
We add comments to our code. Comments are very important to make code more readable and to leave remarks in our code. JavaScript does not execute the comment part of our code. Any text starts with // in JavaScript is a comment or anything enclose like this /* */ is a comment.
5 years ago
**Example: Single Line Comment**
5 years ago
5 years ago
// This is the first comment
// This is the second comment
// I am a single line comment
**Example: Multiline Comment**
5 years ago
/*
5 years ago
This is a multiline comment
Multiline comments can take multiple lines
JavaScript is the language of the web
5 years ago
*/
5 years ago
##### Syntax
5 years ago
JavaScript is a programming language. As a result, it has its syntax like other programming languages. If we do not write a syntax that JavaScript understands, it will raise different types of errors. We will explore different kinds of JavaScript errors later. For now, let us see syntax errors.
5 years ago
![Error](images/raising_syntax_error.png)
I made a deliberate mistake. As a result, the console raises a syntax error. Actually, the syntax is very informative. It informs what type of mistake we made. By reading the error feedback guideline, we can correct the syntax and fix the problem. The process of identifying and removing errors from a program is called debugging. Let us fix the errors:
5 years ago
```js
console.log("Hello, World!")
console.log('Hello, World!')
```
So far, we saw how to display text using a *console.log()*. If we are printing text or string using *console.log()*, the text has to be under the single, double, or backtick.
5 years ago
**Example:**
5 years ago
5 years ago
```js
console.log("Hello, World!")
console.log('Hello, World!')
console.log(`Hello, World!`)
5 years ago
```
5 years ago
#### Arithmetics
5 years ago
Now, let us practice more writing JavaScript codes using *console.log()* on google chrome console for number data types.
In addition to the text, we can also do mathematical calculations using JavaScript. Let us do the following simple calculations.
5 years ago
![Arithmetic](images/arithmetic.png)
5 years ago
5 years ago
```js
console.log(2 + 3) // Addition
console.log(3 - 2) // Subtraction
console.log(2 * 3) // Multiplication
console.log(3 / 2) // Division
5 years ago
console.log(3 % 2) // Modulus - finding remainder
5 years ago
console.log(3 ** 2) // Exponential
5 years ago
```
5 years ago
5 years ago
### Code Editor
5 years ago
We can write our codes on the browser console, but it won't be for bigger projects. In a real working environment, developers use different code editors to write their codes. In this 30 days python JavaScript challenge, we will use visual studio code.
5 years ago
#### Installing Visual Studio Code
5 years ago
VVisual studio code is a very popular open-source text editor. I would recommend to [download](https://code.visualstudio.com/) visual studio code, but if you are in favor of other editors, feel free to follow with what you have.
5 years ago
5 years ago
![Vscode](images/vscode.png)
5 years ago
If you installed visual studio code, let us start using it.
5 years ago
5 years ago
#### How to use visual studio code
5 years ago
Open the visual studio code by double-clicking the visual studio icon. When you open it, you will get this kind of interface. Try to interact with the labeled icons.
5 years ago
![Vscode ui](./images/vscode_ui.png)
5 years ago
5 years ago
![Vscode add project](./images/adding_project_to_vscode.png)
5 years ago
5 years ago
![Vscode open project](./images/opening_project_on_vscode.png)
5 years ago
5 years ago
![script file](images/scripts_on_vscode.png)
5 years ago
5 years ago
![running script](./images/running_script.png)
5 years ago
5 years ago
![coding running](./images/launched_on_new_tab.png)
5 years ago
## Adding JavaScript to a web page
5 years ago
JavaScript can be added to a web page in three different ways:
5 years ago
- **_Inline script_**
- **_Internal script_**
- **_External script_**
- **_Multiple External scripts_**
The following sections show different ways of adding JavaScript code to your web page.
5 years ago
### Inline Script
5 years ago
Create a folder on your desktop and call it 30DaysOfJS or in any location and create an **_index.html_** file in the project folder. Then paste the following code and open it in a browser, either in [Chrome](https://www.google.com/chrome/).
5 years ago
```html
<!DOCTYPE html>
<html>
<head>
<title>30DaysOfScript:Inline Script</title>
</head>
<body>
<button onclick="alert('Welcome to 30DaysOfJavaScript!')">Click Me</button>
5 years ago
</body>
</html>
```
5 years ago
Now, you wrote your first inline script. We can create a pop up alert message using the built-in *alert()* function.
5 years ago
5 years ago
### Internal script
5 years ago
The internal script can be written in the _head_ or the _body_, but it is preferred to put it on the body of the HTML document.
First, let us write on the head part of the page.
5 years ago
```html
<!DOCTYPE html>
<html>
<head>
<title>30DaysOfScript:Internal Script</title>
<script>
console.log("Welcome to 30DaysOfJavaScript")
5 years ago
</script>
</head>
<body>
</body>
</html>
```
5 years ago
This is how we write the internal script most of the time. Writing the JavaScript code in the body section is the most preferred place. Open the browser console to see the output from the console.log()
5 years ago
```html
<!DOCTYPE html>
<html>
<head>
<title>30DaysOfScript:Internal Script</title>
</head>
<body>
<button onclick="alert('Welcome to 30DaysOfJavaScript!');">Click Me</button>
<script>
console.log("Welcome to 30DaysOfJavaScript");
</script>
</body>
</html>
```
5 years ago
Open the browser console to see the output from the console.log()
5 years ago
5 years ago
![js code from vscode](./images/js_code_vscode.png)
5 years ago
5 years ago
### External script
5 years ago
Similar to the internal script, the external script link can be on the header or body, but it is preferred to put it in the body.
First, we should create an external JavaScript file with .js extension. Any JavaScript file ends with .js. Create a file introduction.js inside your project directory and write the following code and link this .js file at the bottom of the body.
5 years ago
5 years ago
```js
console.log('Welcome to 30DaysOfJavaScript')
```
External scripts in the head
5 years ago
```html
<!DOCTYPE html>
<html>
<head>
<title>30DaysOfJavaScript:External script</title>
<script src="introduction.js"></script>
</head>
<body>
</body>
5 years ago
</html>
5 years ago
```
5 years ago
External scripts in the body
5 years ago
5 years ago
```html
<!DOCTYPE html>
<html>
<head>
<title>30DaysOfJavaScript:External script</title>
</head>
<body>
5 years ago
//it could be in the header or in the body
5 years ago
// Here is the recommended place to put the external script
<script src="introduction.js"></script>
</body>
5 years ago
</html>
5 years ago
```
5 years ago
Open the browser console to see the output from the console.log()
5 years ago
5 years ago
### Multiple External scripts
5 years ago
5 years ago
We can link multiple external JavaScript files to a web page.
Create a helloworld.js file inside the 30DaysOfJS folder and write the following code.
5 years ago
5 years ago
```js
console.log('Hello, World!')
```
5 years ago
5 years ago
```html
<!DOCTYPE html>
<html>
<head>
<title>Multiple External Scripts</title>
</head>
<body>
<script src ="./helloworld.js"></script>
<script src="./introduction.js"></script>
5 years ago
</body>
5 years ago
</html>
5 years ago
```
5 years ago
5 years ago
Your main.js file should be below all other scripts. Watch out your exercise needs to understand this line.
5 years ago
5 years ago
![Multiple Script](./images/multiple_script.png)
5 years ago
## Introduction to Data types
5 years ago
In JavaScript and also other programming languages, there are different kinds of data types. The following are JavaScript primitive data types:_String, Number, Boolean, undefined, Null_, and _Symbol_.
5 years ago
### Number
5 years ago
5 years ago
- Integer: Integer(negative, zero and positive) numbers
5 years ago
Example:
... -3, -2, -1, 0, 1, 2, 3 ...
5 years ago
- Float: Decimal number
5 years ago
Example
... -3.5, -2.25, -1.0, 0.0, 1.1, 2.2, 3.5 ...
5 years ago
### String
5 years ago
A collection of one or more characters under a single quote, double-quote, or backtick.
5 years ago
**Example:**
```js
'Asabeneh'
'Finland'
5 years ago
'JavaScript is a beautiful programming language'
"I love teaching"
'I hope you are enjoying the first day'
`We can also create a string using a backtick`
5 years ago
```
5 years ago
### Booleans
5 years ago
A boolean value is either true or false. Any comparisons return a boolean value, which is either true or false.
5 years ago
A boolean data type is either a True or False value.
**Example:**
```js
5 years ago
true // if the light on ,the value is true
false // if the light off, the value is False
5 years ago
```
5 years ago
### Undefined
5 years ago
In JavaScript, if we don't assign a value to a variable, the value is undefined. In addition to that, if a function is not returning anything, it returns undefined.
5 years ago
```js
let firstName;
console.log(firstName); //not defined, because it is not assigned to a value yet
```
5 years ago
### Null
5 years ago
5 years ago
Null in JavaScript means an empty value.
5 years ago
```js
5 years ago
let emptyValue = null
5 years ago
```
5 years ago
## Checking Data types
To check the data type of a certain data type, we use the **typeof** operator. See the following example.
5 years ago
5 years ago
```js
console.log(typeof 'Asabeneh') // string
console.log(typeof 5) // number
5 years ago
console.log(typeof true ) // boolean
console.log(typeof null) // object type
5 years ago
console.log(typeof undefined) // undefined
```
5 years ago
## Comments
5 years ago
Commenting in JavaScript is similar to other programming languages. Comments are important in making your make code more readable.
5 years ago
There are two ways of commenting:
5 years ago
5 years ago
- _Single line commenting_
- _Multiline commenting_
```js
5 years ago
// commenting the code itself with a single comment
5 years ago
// let firstName = 'Asabeneh'; single line comment
// let lastName = 'Yetayeh'; single line comment
```
5 years ago
5 years ago
Multiline commenting:
```js
/*
let location = 'Helsinki';
let age = 100;
let isMarried = true;
This is a Multiple line comment
*/
```
5 years ago
## Variables
5 years ago
Variables are _containers_ of data. Variables used to _store_ data in a memory location. When a variable is declared, a memory location is reserved. When a variable is assigned to a value (data), the memory space will be filled with that data. To declare a variable, we use _var_, _let_, or _const_ keywords. We will talk more about var, let, and const in detail in other sections (scope). For now, the above explanation is enough.
5 years ago
For a variable that changes at a different time, we use _let_. If the data does not change at all, we use _const_. For example, PI, country name, gravity do no change, and we can use *const*.
5 years ago
5 years ago
- A JavaScript variable name should not begin with a number.
- A JavaScript variable name does not allow special characters except dollar sign and underscore.
- A JavaScript variable name follows a camelCase convention.
- A JavaScript variable name should not have space between words.
5 years ago
5 years ago
The following are valid examples of JavaScript variables.
5 years ago
Valid variables in JavaScript:
5 years ago
5 years ago
```js
firstName
lastName
country
city
capitalCity
age
isMarried
first_name
last_name
is_marreid
capital_city
num1
num_1
_num_1
$num1
5 years ago
year2020
year_2020
5 years ago
```
5 years ago
5 years ago
camelCase or the first way of declaring is conventional in JavaScript. In this material, we will use camelCase variables.
5 years ago
Invalid variable:
5 years ago
5 years ago
```sh
5 years ago
first-name
1_num
num_#_1
```
5 years ago
Let us declare variables with different data types. To declare a variable, we need to use let or const keyword before the variable name. Following the variable name, we write an equal sign (assignment operator), and a value.
5 years ago
```js
# Syntax
let nameOfVariable = value
```
5 years ago
5 years ago
**Examples: Variables**
5 years ago
5 years ago
```js
// Declaring different variables of different data types
5 years ago
let firstName = 'Asabeneh' // first name of a person
let lastName = 'Yetayeh' // last name of a person
let country = 'Finland' // country
let city = 'Helsinki' // capital city
5 years ago
let isMarried = true // boolean data type
5 years ago
5 years ago
console.log(firstName, lastName, country, city, isMarried)
5 years ago
```
```sh
Asabeneh Yetayeh Finland Helsinki 100 True
```
5 years ago
5 years ago
```js
5 years ago
// Declaring variables with number values
5 years ago
let age = 100 // age in years
5 years ago
const gravity = 9.81 // earth gravity in m/s2
const boilingPoint = 100 // water boiling point, temperature in oC
const PI = 3.14 // geometrical constant
5 years ago
console.log(gravity, boilingPoint, PI)
```
```sh
9.81 100 3.14
```
```js
5 years ago
// Variables can also be declaring in one line separated by comma
5 years ago
let name = 'Asabeneh', // name of a person
5 years ago
job = 'teacher',
live = 'Finland';
console.log(name, job, live);
```
5 years ago
5 years ago
```sh
Asabeneh teacher Finland
```
5 years ago
When you run the files on 01-Day folder you should get this:
5 years ago
![Day one](./images/day_1.png)
5 years ago
🌕 You are amazing. You have just completed day 1 challenge and you are in your way to greatness. Now do some exercises for your brain and for your muscle.
5 years ago
# 💻 Day 1: Exercises
5 years ago
5 years ago
1. Write a single line comment which says, _comments can make code readable_
2. Write another single comment which says, *welcome to 30DaysOfJavaScript*
3. Write a multiline comment which says, _comments can make code readable, easy to use_
_and informative_
4. Create a variable.js file and declare variables and assign string, boolean, undefined and null data types
5. Create datatypes.js file and use the JavaScript ***typeof*** operator to check different data types. Check the data type of each variable
5 years ago
6. Declare four variables without assigning values
7. Declare four variables with assigning values
8. Declare variables to store your first name, last name, marital status, country and age in multiple lines
9. Declare variables to store your first name, last name, marital status, country and age in a single line
10. Declare two variables _myAge_ and _yourAge_ and assign them initial values and log to the browser console.
5 years ago
5 years ago
```sh
I am 25 years old.
You are 30 years old.
```
5 years ago
🎉 CONGRATULATIONS ! 🎉
[Day 2 >>](https://github.com/Asabeneh/30DaysOfJavaScript/blob/master/02_Day/02_day_data_types.md)