Skip to content

Commit

Permalink
Merge branch 'main' into html_boilerplate_formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
vamsijv committed Aug 21, 2024
2 parents ae16160 + 569e638 commit 8aa0907
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 44 deletions.
127 changes: 88 additions & 39 deletions foundations/javascript_basics/variables_and_operators.md
Original file line number Diff line number Diff line change
@@ -1,86 +1,130 @@
### Introduction

In the previous sections you learned how to structure webpages with HTML and style them with CSS. The next step is to make the webpage *interactive*, which is exactly what JavaScript is for. In this section, we will focus on the fundamentals of JavaScript and how you can use it to manipulate all the various interactions between the web page and user.
In the previous sections you learned how to structure webpages with HTML and style them with CSS. The next step is to make the webpage *interactive*, which is exactly what JavaScript is for.

In this section, we will focus on the fundamentals of JavaScript and how you can use it to manipulate all the various interactions between the web page and user.

### Lesson overview

This section contains a general overview of topics that you will learn in this lesson.

- How do you declare a variable?
- What are three different ways to declare a variable?
- Which one should you use when?
- What are the rules for naming variables?
- What are operators, operands, and operations?
- What is concatenation and what happens when you add numbers and strings together?
- What are the different types of operators in JavaScript?
- What is the difference between == and ===?
- What are operator precedence values?
- What are the increment/decrement operators?
- What is the difference between prefixing and postfixing them?
- What are assignment operators?
- What is the Unary Plus Operator?
- Running JavaScript code using an HTML file and via the browser console.
- Declaring variables with `let` and `const`.
- Performing number operations
- Performing string operations
- Use of logical and mathematical operators

### How to run JavaScript code

All JavaScript we will be writing in the majority of the Foundations course will be run via the browser. Later lessons in Foundations and the NodeJS path will show you how to run JavaScript outside of the browser environment. Outside of these lessons, for now you should always default to running your JavaScript in the browser unless otherwise specified, otherwise you may run into unexpected errors.
All JavaScript we will be writing in the majority of the Foundations course will be run via the browser. Later lessons in Foundations and the NodeJS path will show you how to run JavaScript outside of the browser environment.

Outside of these lessons, for now you should always default to running your JavaScript in the browser unless otherwise specified, otherwise you may run into unexpected errors.

The simplest way to get started is to create an HTML file with the JavaScript code inside of it. Type the basic HTML skeleton into a file on your computer somewhere:
The simplest way to get started is to create an HTML file with the JavaScript code inside of it. Use the VS Code snippet <kbd>!</kbd> + <kbd>TAB</kbd> to create the basic HTML skeleton in a file on your computer somewhere. Be sure to include the `<script>` tag:

```html
<!DOCTYPE html>
<html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>

<script>
// Your JavaScript goes here!
console.log("Hello, World!")
</script>

</body>
</html>
```

Save and open this file up in a web browser (you can use ["Live Server" on Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer) to do this!) and then <span id="access-devTools-console">open up the browser's console by right-clicking on the blank webpage and selecting "Inspect" or "Inspect Element". In the 'Developer Tools' pane find and select the 'Console' tab</span>, where you should see the output of our `console.log` statement.
Save and open this file up in a web browser and then open up the browser's console:

> <span id="console-log">`console.log()` is the command to print something to the developer console in your browser. You can use this to print the results from any of the following articles and exercises to the console.</span> We encourage you to code along with all of the examples in this and future lessons.
1. Right-click on the blank webpage.
1. Click on "Inspect" or "Inspect Element" to open the Developer Tools.
1. Find and select the "Console" tab, where you should see the output of our `console.log` statement.

**Tip:** You can use ["Live Server" extension in Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer) to automatically update the browser when you save your file instead of having to manually refresh the page to see any changes when you edit your code. Try edit the text to say something different!

`console.log()` is the command to print something to the developer console in your browser. You can use this to print the results from any of the following articles and exercises to the console. We encourage you to code along with all of the examples in this and future lessons.

Another way to include JavaScript in a webpage is through an external script. This is very similar to linking external CSS docs to your website.

```html
<script src="javascript.js"></script>
<script src="javascript.js"></script>
```

JavaScript files have the extension `.js` similar to `.css` for stylesheets. External JavaScript files are used for more complex scripts.

We named our file `javascript.js` but we could have chosen any name like `my-script.js` or even no name `.js`. What is really important is the `.js` extension.

### Variables

You can think of variables as "storage containers" for data in your code.
These are the building blocks of any program, you can think of variables as "storage containers" for data in your code.

![Variable Box Illustration](https://cdn.statically.io/gh/TheOdinProject/curriculum/d39eaf2ca95e80705f703bb218216c10508f5047/foundations/javascript_basics/fundamentals-1/imgs/00.png)

<span id="variable-declaration">Until recently there was only one way to create a variable in JavaScript &mdash; the `var` statement. But in the newest JavaScript versions we have two more ways &mdash; `let` and `const`.</span>
You can declare variables using the `let` keyword. Lets try it! (No pun intended).

```javascript
let name = "John";
let surname = "Doe";

console.log(name);
console.log(surname);
```

What will the `console.log` output? Try it out!

You can also re-assign variables:

```javascript
let age = 11;
console.log(age); // outputs 11 to the console

age = 54;

console.log(age); // what will be output now?
```

Notice the lack of `let` on line 4 - we don't need it since the variable has already been *declared* earlier and we are just re-assigning it here!

1. This tutorial on [JavaScript variables](http://javascript.info/variables) will explain everything you need to know! Be sure to do the **Tasks** at the end. Information won't stick without practice!
Re-assigning is cool and all, but what if we *don't* want it to happen? For example we might have a *constant* `pi` which will never change. We can accomplish this using the `const` keyword.

The above tutorial mentioned this, but it's important enough to note again: `let` and `const` are both relatively new ways to declare variables in JavaScript. <span id="avoid-var">In *many* tutorials (and code) across the internet you're likely to encounter `var` statements. Don't let it bother you! There's nothing inherently wrong with `var`, and in most cases `var` and `let` behave the same way. But sometimes the behavior of `var` is *not* what you would expect. Just stick to `let` (and `const`) for now.</span>
```javascript
const pi = 3.14;
pi = 10;

console.log(pi); // What will be output?
```

Your intuition may tell you that `3.14` will be output. Try it!

An error is thrown. It doesn't even reach the `console.log`! You may wonder why we would *want* an error in our code. Truth be told, errors are incredibly helpful at telling us what is wrong with our code and exactly where the issue is. Without them, our code would still not do what we may want it to, but it would be a major pain to try and find what's wrong!

So in summary, there are two ways to declare a variable:

- `let`, which we can re-assign.
- `const` which we **can't** re-assign and will throw an error if we try.

There is also a third way, `var`, which was the original way variables were declared in JavaScript. `var` is similar to `let` in that variables assigned this way can be reassigned, but it has other quirks that were cleared up when the language introduced `let` and `const`. By and large, it is not used anymore. However, you will likely come across code which uses `var` at some point, so it is useful to know that it exists.

### Numbers

Numbers are the building blocks of programming logic! In fact, it's hard to think of any useful programming task that doesn't involve at least a little basic math... so knowing how numbers work is obviously quite important. Luckily, it's also fairly straightforward.

1. This W3Schools lesson on [JavaScript arithmetic](https://www.w3schools.com/js/js_arithmetic.asp) followed by this on [JavaScript numbers](https://www.w3schools.com/js/js_numbers.asp), are good introductions to what you can accomplish with numbers in JavaScript.
1. This MDN article on [JavaScript math](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Math) covers the same info from a slightly different point of view, while also teaching you how to apply some basic math in JavaScript. There's much more that you can do with numbers, but this is all you need at the moment.
1. Read through \(and code along with!\) this article on [JavaScript operators](http://javascript.info/operators). Don't forget to do the "Tasks" at the bottom of the page! It will give you a pretty good idea of what you can accomplish with numbers (among other things!) in JavaScript.
If you went to school, you will likely not find the concept too difficult to grasp. For example, the mathematical expression `(3 + 2) - 76 * (1 + 1)` is also valid JavaScript. If you put that into a `console.log`, it'll evaluate the expression and output the correct number. Try it!

### Assignment

<div class="lesson-content__panel" markdown="1">

Try the following exercises (and don't forget to use `console.log()`!):

1. Add 2 numbers together! (just type `console.log(23 + 97)` into your HTML file)
1. Add 2 numbers together! (just type `console.log(23 + 97)` into your HTML file or the browser console)
1. Add a sequence of 6 different numbers together.
1. Print the value of the following expression: `(4 + 6 + 9) / 77`
- Answer should be approximately `0.24675`
Expand All @@ -89,13 +133,19 @@ Try the following exercises (and don't forget to use `console.log()`!):
- In the console `console.log(a)` should print `10`
- Try the following in the console: `9 * a`
- and this: `let b = 7 * a` (returns `undefined` \*) and then `console.log(b)`
1. You should be getting the hang of this by now... try this sequence:
- Declare a constant variable `MAX` with the value `57`
- Set another variable `actual` to `MAX - 13`
- Set another variable `percentage` to `actual / MAX`
1. Try this sequence:
- Declare a constant variable `max` with the value `57`
- Set another variable `actual` to `max - 13`
- Set another variable `percentage` to `actual / max`
- If you type `percentage` in the console and press <kbd>Enter</kbd> you should see a value like `0.7719`
1. Take a few minutes to keep playing around with various things in your script tag. Eventually, we will learn how to actually make those numbers and things show up on the webpage, but all of this logic will remain the same, so make sure you're comfortable with it before moving on.

Go through the following articles to deepen your knowledge.

1. This W3Schools lesson on [JavaScript arithmetic](https://www.w3schools.com/js/js_arithmetic.asp) followed by this on [JavaScript numbers](https://www.w3schools.com/js/js_numbers.asp), are good introductions to what you can accomplish with numbers in JavaScript.
1. This MDN article on [JavaScript math](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Math) covers the same info from a slightly different point of view, while also teaching you how to apply some basic math in JavaScript. There's much more that you can do with numbers, but this is all you need at the moment.
1. Read through \(and code along with!\) this article on [JavaScript operators](http://javascript.info/operators). Don't forget to do the "Tasks" at the bottom of the page! It will give you a pretty good idea of what you can accomplish with numbers (among other things!) in JavaScript.

<div class="lesson-note lesson-note--tip" markdown="1">

As you might have noticed by running JavaScript code in the console, the console prints the result of the code it executes (called a return statement). You will learn more about these in the next lessons, however for now it is good to remember that a declaration with an assignment (such as `let b = 7 * a`) returns `undefined` and so you cannot declare and assign a value to a variable and read its value in the same line.
Expand All @@ -108,23 +158,22 @@ As you might have noticed by running JavaScript code in the console, the console

The following questions are an opportunity to reflect on key topics in this lesson. If you can't answer a question, click on it to review the material, but keep in mind you are not expected to memorize or master this knowledge.

- [Name the three ways to declare a variable](#variable-declaration)
- [Which of the three variable declarations should you avoid and why?](#avoid-var)
- [Name the three ways to declare a variable](#variables)
- [Which of the three variable declarations should you avoid and why?](#variables)
- [What rules should you follow when naming variables?](https://javascript.info/variables#variable-naming)
- [What happens when you add numbers and strings together?](https://javascript.info/operators#string-concatenation-with-binary)
- [How does the Modulo (%), or Remainder, operator work?](https://javascript.info/operators#remainder)
- [Explain the difference between `==` and `===`.](https://www.w3schools.com/js/js_numbers.asp)
- [Explain the difference between `==` and `===`.](https://www.w3schools.com/js/js_operators.asp)
- [When would you receive a `NaN` result?](https://www.w3schools.com/js/js_numbers.asp)
- [How do you increment and decrement a number?](https://javascript.info/operators#increment-decrement)
- [Explain the difference between prefixing and postfixing increment/decrement operators.](https://javascript.info/operators#increment-decrement)
- [What is operator precedence and how is it handled in JS?](https://javascript.info/operators#operator-precedence)
- [How do you access developer tools and the console?](#access-devTools-console)
- [How do you log information to the console?](#console-log)
- [How do you access developer tools and the console?](#how-to-run-javascript-code)
- [How do you log information to the console?](#how-to-run-javascript-code)
- [What does unary plus operator do to string representations of integers? eg. +"10"](https://javascript.info/operators#numeric-conversion-unary)

### Additional resources

This section contains helpful links to related content. It isn't required, so consider it supplemental.

- The differences between `var` and `let` are explained in this JavaScript.info article titled the [old "var"](https://javascript.info/var).
- This [MDN article on what is JavaScript](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/What_is_JavaScript) explains a bit more about it on a high-level.
4 changes: 2 additions & 2 deletions nodeJS/express/installing_postgresql.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ You'll be prompted to enter a password and to verify it. Once you are done, the
GRANT ALL PRIVILEGES ON DATABASE <role_database_name> TO <role_name>;
```

Remember that you should change the `<role_database_name>` and `<role_name>` (they should be both the same)! If you see `GRANT` in response to the command, then you can type `\q` to exit the prompt.
Remember that you should change the `<role_database_name>` and `<role_name>` (they should both be the same)! If you see `GRANT` in response to the command, then you can type `\q` to exit the prompt.

#### 3.5 Saving access information in the environment

Expand Down Expand Up @@ -259,7 +259,7 @@ You'll be prompted to enter a password and to verify it. Once you are done, the
GRANT ALL PRIVILEGES ON DATABASE <role_database_name> TO <role_name>;
```

Remember that you should change the `<role_database_name>` and `<role_name>` (they should both the same)! If you see `GRANT` in response to the command, then you can type `\q` to exit the prompt.
Remember that you should change the `<role_database_name>` and `<role_name>` (they should both be the same)! If you see `GRANT` in response to the command, then you can type `\q` to exit the prompt.

#### 3.4 Saving access information in the environment

Expand Down
8 changes: 8 additions & 0 deletions nodeJS/orms/prisma_orm.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ You might have a complex query that you just are unable to get right via the Pri

Prisma migrate is a tool that helps you perform database migrations. You won't be using it a whole ton in the curriculum, but it's good to be aware of it. When you decide to change the schema in any way, you run a Prisma migration to apply the schema changes to the database. These changes are tracked in a `migrations` folder in your codebase.

<div class="lesson-note lesson-note--warning" markdown="1">

#### Prisma ORM limitations

In the [Using PostgreSQL lesson](https://www.theodinproject.com/lessons/nodejs-using-postgresql), we learned about Identity columns. PostgreSQL recommends the use of Identity columns, as they comply with the SQL standard. Prisma ORM, however, does not support these columns, and will create PostgreSQL specific [Serial Types](https://www.postgresql.org/docs/16/datatype-numeric.html#DATATYPE-SERIAL) instead. This most likely will not affect your projects, but it can be important to keep in mind. See this Stackoverflow answer for a short description on the [difference between Serial and Identity](https://stackoverflow.com/a/55300741/1882858).

</div>

### Assignment

<div class="lesson-content__panel" markdown="1">
Expand Down
1 change: 0 additions & 1 deletion ruby/basic_ruby/conditional_logic.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,4 +343,3 @@ This section contains helpful links to related content. It isn't required, so co

- See this [Stack Overflow post for more on the spaceship operator](https://stackoverflow.com/questions/827649/what-is-the-ruby-spaceship-operator).
- For more depth on flow control, read [Zetcode's Flow Control section](http://zetcode.com/lang/rubytutorial/flowcontrol/).
- If you want some in-depth practice with these concepts, go through [Learn Ruby the Hard Way](https://learnrubythehardway.org/book/) from Exercise 27 through Exercise 31.
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ You'll be prompted to enter a password and to verify it. Once you are done, the
GRANT ALL PRIVILEGES ON DATABASE <role_database_name> TO <role_name>;
```

Remember that you should change the `<role_database_name>` and `<role_name>` (they should both the same)! If you see `GRANT` in response to the command, then you can type `\q` to exit the prompt.
Remember that you should change the `<role_database_name>` and `<role_name>` (they should both be the same)! If you see `GRANT` in response to the command, then you can type `\q` to exit the prompt.

#### 3.5 Saving access information in the environment

Expand Down Expand Up @@ -283,7 +283,7 @@ You'll be prompted to enter a password and to verify it. Once you are done, the
GRANT ALL PRIVILEGES ON DATABASE <role_database_name> TO <role_name>;
```

Remember that you should change the `<role_database_name>` and `<role_name>` (they should both the same)! If you see `GRANT` in response to the command, then you can type `\q` to exit the prompt.
Remember that you should change the `<role_database_name>` and `<role_name>` (they should both be the same)! If you see `GRANT` in response to the command, then you can type `\q` to exit the prompt.

#### 3.4 Saving access information in the environment

Expand Down

0 comments on commit 8aa0907

Please sign in to comment.