I'm challenging myself to learn JavaScript in 30 Days from Scratch. Ill be uploading the resources here and my thoughts on the same
-
- Data types
- Boolean :
trueorfalse - String :
'Hello World' - Number :
42,-9.3 - Math Functions and Operators
- Arithmetic:
+, - , * /
- Arithmetic:
- Uses
console.log('Print Something Here')<button onclick="Action">Click Me</button>alert('Message Pop up')
-
- Variable Naming Conventions
- Use meaningful names for variables
- Avoid using reserved words as variable names (
var if = false;) - Declare your variables with the appropriate data type (
letorconst). Constants should be used when you know that a value will - Declare your variables with the appropriate data type (e.g.,
let name = "John";) - You can declare multiple variables in one line by separating them with commas (
let x=1, y=2;). - JavaScript is case sensitive (
VarNameIsDifferentFrom varnameisdifferentfrom).
- Let and Const
- Let allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used.
- Const lets you declare a variable that is constant and its value cannot be reassigned once it's set.
- Start with an empty cart.
- Add items to the cart (+1).
- Increment by 2.
- Increment by 3.
- Reset Cart.
- Variable Naming Conventions
-
-
Booleans are
trueorfalsevalues. -
They are used in conditional statements like If Else Statements.
==- equals to!=- not equal to===- Checks for equality in both logic and datatype>=- greater than or equal to<=- less than or equal to
-
Logical operators
&&AND||OR!NOT. -
if Else statements
-
Falsy Values
- false
- 0
- null
- undefined
- '' (empty string)
- NaN (Not a number)
Implement Rock Paper Scissors game.
- Prompt user to choose either rock, paper, or scissors.
- Generate computer's choice randomly.
Math.random() - Compare the two choices.
- If they are the same print draw.
- If one wins over the other print winner.
- If none win print tie.
-
-
- Functions allow you to package code so that it can be reused throughout your program.
- Syntax:
function function_name(parameter1, parameter2){} - Parameter list
( )is optional if there are no parameters. - To call a function use its name followed by parentheses
(). - The value returned from a function is stored in a special variable called
return. - Functions can also return nothing by leaving out the
returnstatement entirely. This returnsundefined, which is treated asfalsein - A function can return anything including objects, arrays, strings, numbers etc.
- If nothing is returned then JavaScript returns
undefined. - Global scope is where all variables exist unless declared otherwise using
var,let, orconst. - Local scope is created when a function is executed. It exists only while the function is being run.
- Wrap the prompts into a separate function.
- Use arrow functions which have shorter syntax.
-
- An object is a collection of key value pairs.
- Syntax:
const objectName = { propertyName : "value", anotherProperty : "anotherValue", };
- You can access properties with dot notation
.console.log(objectName.propertyName);
- Or bracket notation
[ ]console.log(objectName["propertyName"]); // Same as above.
- Objects are dynamic meaning new properties can be added at any time.
- Methods are just like properties but instead of holding values they hold functions.
- Why we use Objects
- Store related data together.
- Group functionality together.
- Makes code easier to read and manage.
Add a score tracker to the Rock Paper scissors game
- Create an object named
scorewith properties for both player and computer scores. - Whenever someone wins update their respective score.
- Create a
Reset scorebutton that resets all scores to0.
-
- Web storage API provides a way to store data on the client side.
- There are two types of web storage
localStorageandsessionStorage. localStoragestores data even after closing browser tab/window.sessionStoragewill remove data once closed.- Both work in similar fashion except data persists longer.
-
Document Object Model (DOM)
- Document Object Model allows us to interact with HTML elements in our webpage.
- Every element on a page is represented by an object in the DOM.
- We can manipulate these objects to change how a webpage looks and behaves.
- Example manipulating paragraph text color:
Build an Amazon Calculator Project
- Ask for Cost Value.
- Orders under $40 = +$10 shipping.
- Orders over $40 = FREE shipping.
- Print out the Total Cost after Shipping fees added
-
-
Arrays are ordered collections of items.
-
They can contain different types of data such as numbers, strings, objects, arrays etc.
-
Array methods allow you to perform operations on arrays.
-
Syntax for arrays
const arrayName = [value1, value2, value3];
-
Accessing array elements
- Using brackets
arrayName[index] - Dot Notation
arrayName.index
- Using brackets
-
Examples include
push(),pop(),shift(),unshift().push()adds one or many items to the end of array.pop()removes item from the end of array.shift()removes first item in array.unshift()adds one or many items to beginning of array.
-
Array method chaining is when you call multiple methods one right after the other.
- Create an array to store todos.
- Prompt user to enter todo.
- User should be able to add new todos.
-
-
- Loops allow us to repeat actions a set number of times.
- While Loops
- Execute a piece of code as long as the conditions remain True
- Syntax
while (condition) { code };
- For Loops
- Execute a piece of code for a limited number of time
- Syntax
for (init; condition; incr/decrement) { code };