Welcome to the Javascript activity page! Here you will learn step-by-step how to code in Javascript!
Look for the text in bold for basic instructions and information; the regular text will give more details if you need them.
To start programming in Javascript you need to tell the browser the location of the Javascript. To do that put the <script>
tag code right before the </body>
tag:
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> <script>
</script>
console.log("Hello World!")
between the tags <script></script>
.
console.log("")
tells Javascript what it is supposed to do (in this case, print something to the log something out to the screen)."Hello World!"
tells Javascript what is supposed to be printed to screen.var x = prompt('What is your name?')
underneath console.log("Hello World!")
.
x
.console.log("Your name is " + x)
.var x = prompt('What is your name?')
to store the user's name into x
—x
is an example of a variable.var
and unique variable name and assign it a value using the = sign.
var num = 1
means that we have made a variable called num and assigned it a value of 1.var myfloat = 7.2
.var str_hw = "Hello World!"
or var mystring = 'hello'
.var x = 1
under the Javascript from the previous section.console.log(x)
. You see that the console at the bottom of the screen prints 1.var y = 2
.var z = x + y
, and output the results with console.log(z)
=
sign. For example what does the following lines of code display?
var i = 1 console.log(i) i = 2 console.log(i)
prompt("")
and ask for the favourite color. Create a variable and assign it to be the prompted favourite color. Then print the number to console or the webpage.console.log()
. Try using as few lines as possible while only using 5 and 10 one time.
var food = prompt("What is your favourite food?") if (food == "spam"){ console.log("Ummmm, my favourite!") }
var food = prompt('What is your favourite food?') if (food == "spam"){ console.log("Ummmm, my favourite!") console.log("But not spam EMAIL.") } else{ console.log("No, I won't have it. I want spam!") }
for(var i = 1; i <= 10; i++){ console.log(i) }
for(var i = 1; i <= 10; i++)
now.
var i = 1
creates a variable i
that has the value 1.i<=10
makes the loop keep happening until i
equals 10.i++
adds 1 to i
at the end of the for loop which marked by the curly brace }
. To decrease the number by onevar list1 = ['physics', 'chemistry', 1997, 2000]
var list2 = [1, 2, 3, 4, 5]
var list3 = ["a", "b", "c", "d"]
console.log(list3[3])
console.log(list3[0])
, and to print the last element, console.log(list3[3])
.
for(var i = 0; i < list1.length; i++){ console.log(list1[i]) }
list1.length
is the length of the list. You can use it to find the length of strings too.
i--
at the end of the for loop statement.
var list = [1, 2, 3, 4, 5] for(var i = list.length; i >= 0; i--){ if(i < 3){ console.log("we found " + list[i]) } }
i
is less than 5?<script></script>
:function sayHello(){ console.log("hello") }
function
()
{console.log("hello")}
sayHello()
anywhere and you will print "hello" where ever you use the function.for(var i = 1; i<=5; i++){ sayHello() }
function sayWord(word){ console.log("this is a word " + word) } var wordToSay = "hello" sayWord(wordToSay)
number
and will print number.function addNumbers(num1, num2){ var added = num1 + num2 console.log(added) } var number1 = 2 var number2 = 5 var number3 = 1 addNumbers(number1, number2) addNumbers(number1, number3)
[1, 2, 3] returns 6 [1, 2, 13] returns 3 [1, 13, 3] returns 1
Operator | Description | Example |
---|---|---|
== | If the values of two operands are equal, then the condition becomes true. | a == b |
!= | If values of two operands are not equal, then condition becomes true. | a != b |
> | If the value of left operand is greater than the value of right operand, then condition becomes true. | a > b |
< | If the value of left operand is less than the value of right operand, then condition becomes true. | a < b |
>= | If the value of left operand is greater than or equal to the value of right operand, then condition becomes true. | a >= b |
<= | If the value of left operand is less than or equal to the value of right operand, then condition becomes true. | a <= b |