CS174
Chris Pollett
Nov 2, 2020
<script type="text/javascript"> -- code -- </script>
<script type="text/javascript" src="myscript.js" />
return x; // has the effect of just return!
<html>
<head><title>test</title>
<meta name="description" value="this example illustrates how Javascripts are executed both when the document is loaded and on the occurrence of events" />
<script type="text/javascript" >
function sayHello()
{
alert("hi there");
}
</script>
</head><body><form><input type="button" value="test" onclick="return sayHello();"
/><!-- responds to events --></form>
<script type="text/javascript" >
for( i = 0; i<100; i++) {
document.writeln("<p>hi"+i+"</p>");
} // run when document loads
</script></body>
</html>
//Declaring variables with function scope
var my_variable; // don't need to assign a var when declaring it
var pi=3.14; // but can
var a=1,b; //can declare more than one variable in one go
// declaring variables with block scope
let my_other_var; //let first appeared in Ecmascript 2015
let foo = 2;
let c=4,d;
{
let e =10;
var f = 7;
console.log(e + " " + f); //prints 10 7
}
console.log(e); // ReferenceError
console.log(f); // 7
const PI = 3.14;
first = "hello" second = first + "bye" // "hellobye"
"August" + 1977 // "August1977" 1977 + "August" // "1977August" 7*"3" =21
var str_value = String(value); // Might want to use toString var some_number = Number(some_string); // Might want to use parseInt or parseFloat
Which of the following statements is true?
typeof x //and typeof(x)returns either "boolean", "string", "number" if x is of primitive type, it returns "object" if x is null or an object; and it returns "undefined" if x is not defined.
a++; a+=2; a--; a-=2; a = b +57;
var today = new Date();
name = prompt("What is your name", "John Smith")
var my_object = new Object();
delete my_object;
my_object.make= "V6" /* would then give a property make a value. */ //can access as p = my_object["make"] q = my_object.make //to delete a property of an object we can do delete my_object.make
my_object.subObject = new Object();
for (prop in my_object) {
alert("prop:" + prop + "\nvalue:" + my_object[prop]);
}
or (as of Ecmascript 2015)
var value;
for (value of my_object) {
alert("value:" + value);
}
my_object = {
make: "V6",
subObject: {}
}
my_object.hasOwnProperty("make"); //returns true given object above
var myArr = new Array(1, 2, "hello") var myArr = new Array(100); var myArr = [1,2,3]; //to access myArr[0] //to determine length myArr.length
var names = new Array("Mary", "Murray", "Max");
var nstring = names.join(":");
var a = [1, 2, 3]; a.concat(4, 5);
(//we'll talk more about functions in a moment)
function my_callback(item, index) {
alert(index + ":" + item);
}
var a = [1, 2, 3];
a.forEach(my_callback);
function swap(i, j, a)
{
var tmp=a[i]; /* explicitly defined variables
have scope within the function
if I had declared the variable
implicitly it would have global scope */
a[i] = a[j]; a[j] = tmp;
}
swap(10, 5, b);
var c = swap;So could call:
c(10, 5, b);
function swap()
{
var i = this.arguments[0], j=this.arguments[1], a=this.arguments[2];
//same code as before
}
swap = function(i, j, a) {
/* same code as before*/
}