- Access Content
- JS can be used to select any element, attribute, or text from HTML.
- Modify Content
- JS can be used to add or remove any element, attribute, or text from the page
- Program Rules
- You can specify a set of steps for the browser to follow that can change or access the content of a page
- React to Events
- You can specify that a script should tun when a specific event has occurred
Each version of a browser adds new features, but website visitors will not always have the most recent update, so you cannot always rely on the latest technology.
A script is a series of instructions that a computer can follow to achieve a goal, like a recipe. They can run different sections of code in response to the situation around them.
- Writing a Script
- To write a script, you need to state your goal and list the tasks that need to be completed for it to happen.
- Define the goal
- Design the script
- Code each step
- To write a script, you need to state your goal and list the tasks that need to be completed for it to happen.
- Designing a Script: Tasks
- Once the goal of the script is known you can work through the individual tasks necessary.
- Designing a Script: Steps
- Each individual task can be broken down into a series of steps.
- From Steps to Code
- Every step needs to be written into a language the program can follow.
- You need to learn to think like the computer to understand how it will try to solve tasks.
- Sketching out the Tasks in a Flowchart
- Often scripts will need to perform different tasks for different situations, so you can use a flowchart to fit everything together and show the paths between each step.
An expression evaluates into a single value. There are two types of expressions:
- Expressions that just assign a value to a variable
- Expressions that use two or more values to return a single value
Expressions rely on operators, which allow programmers to create a single value from one+ values.
- Addition
+ - Subtraction
- - Division
/ - Multiplication
* - Increment
++ - Decrement
-- - Modulus
%
Several arithmetic operations can be performed in one expression, but it is important to understand the order of operations. Multiplication and division occur before addition or subtraction.
There is only one string operator, the + symbol is used to join strings to other things.
Strings and numbers can be put together into a single string.
Functions let you group a series of statements together to perform a specific task. You can reuse the function to perform the same action again later instead of rewriting the code each time.
Grouping the statements together for certain tasks helps to organize your code, and the statements within a function aren't executed automatically when the page loads, so they can be controlled.
If you need certain info for a function to work, you put the necessary info in as an argument, it transfers into the parameter, and the requested info comes back as the return.
To create a function, you give it a name and write the statements needed to achieve its tasks inside the curly braces. You declare it using the function keyword, a name of your choice, and parenthesis, ie function /name/()
Having declared the function, you can execute the code within by calling it. To run the code, you use the function name followed by parenthesis, ie /name/();
Sometimes a function needs specific info to perform its task, which you can provide by giving it parameters, which act like variables within the function. You indicate parameters within the parenthesis of the function name.
When you call a function that has parameters, you specify the values or variables it should use within the parenthesis that follow its name as an argument.
Some functions return info to the code that called them, this is done using the return keyword.