A “script” is a series of instructions written in a programming language that a machine can read and understand.Once you have written a series of instructions, you can then ‘run’ the script and have the machine perform the operations in the order you specified.
Think of a program like a cookbook and a script like a recipe inside that cookbook. You can have a variety of ingredients you are working with (these could be arguments, variables, and data) and then you use commands in your programming language to manipulate and perform operations on this input (these are the numbered steps in your recipe) and at the end of the script you could have some kind of output (like the finished recipe).
If you want to write your first script today, right here in your web browser, try following these instructions:
Open 'Inspect Element' in your browser by right-clicking, or from your browser's menu. This should open a new window with code inside.
Select the 'Console' or 'JavaScript Console' tab inside your 'Inspect Element' window. You should see a prompt where you can enter code that looks like this:
>
To write your first few scripts, let's try a few things! First, type a-l-e-r-t-(-'-h-e-l-l-o-'-) into the console and press Enter.
alert('hello')
If you're writing this into the JavaScript console you should see a dialog box that says 'Hello' appear from your browser. Congrats! That's your first command.
Now if you were able to type alert('hello') and see a dialog box, let's try entering something more useful.
var price = 24
And now hit Enter. This sets a variable named price to the number 24. Let's pretend you paid $24 for a meal and you were trying to figure out how to add a 15% tip, and then split the total (with tip) between you and a friend.
Let's see if we can work with our price variable to figure out the amount with tip. If we just type price and hit Enter, the console should give us 24 back as a result. If we type price * 2 we should get 48 back, but it's not saving that number anywhere. Let's define a new variable for the total cost of the meal plus tip:
var total = price * 1.15
Now we have another variable set which should equal 27.599999999999998 if we enter total into the console again. The last thing we want to do is figure out how much it costs per person if we split the bill 2 ways, so let's do one last command:
total / 2
We should see 13.799999999999999 if everything has worked!
That whole process above could be written like this:
var price = 24
var total = price * 1.15
total / 2
And we can turn it into a function like this:
function tipCalculator(){
var price = 24
var total = price * 1.15
return total / 2
}
Now that it's a function, we can make it smarter by giving it arguments for our original bill value 24, also we can give it another argument for the desired amount of tip we want to leave instead of always 1.15, and we can also inform the function between how many people we want to split the total. A more advanced version of this same script might look like this:
function tipCalculator(bill,tip,people){
var price = bill
var total = price * ((tip + 100) / 100)
return total / people
}
And we could get the same 13.799999999999999 result from that function as our original script by running it with the following arguments:
tipCalculator(24,15,2)
I hope this helps break down what a script is, how to get started, and how individual commands and variables can be used together to help make functions that you can use to figure things out :D
JavaScript is very exciting, and the best part is you can write it right in your web browser!