JavaScript
What boolean operators does JavaScript support?
&&, || and !
How do you declare a variable?
* Declares empty (no value) variable named 'd': var d * Declares variable and assign a value to it. var dat = 1; Assign text value in quotes, var car12 = "Honda"; numeric value - no quotes. * Declares unchangeable (const) variable with value: const d = 32
How do you specify comments in JavaScript?
// - Single line comments / * ... * / - Multi line comments (begin with slash star and end with star slash)
What are some of the things JavaScript can do to an HTML page?
1. Can react to events(set to execute when a user clicks an element, when the page finishes loading, etc). 2. Can manipulate(read or change) an HTML element. 3. Can validate data(form input). 4. Can detect the visitor's browser(and load another page specifically designed for it). 5. Can create cookies(to store and retrieve information on a visitor's computer).
What is a JavaScript function?
A function is a block of JavaScript code that is defined once but may be executed any number of times by an event such as: - clicking a button; - a call within your script; - a call within another function; etc. A function is placed in <head> or <body> section depending when the call is made.
What does "1"+2+4 evaluate to?
124
How about 2+5+"8"?
78
You should end your lines with what?
;
How would you write 12345 one digit at a time using HTML & JavaScript?
</title> </head> <body> <script type="TEXT/JAVASCRIPT"> var asNumber = 12345; var asText = asNumber.toString(); document.write("Reading/writing " + asText + " one digit at a time as text: "); var x; for (x=0;x<5;x++) { document.write(asText[x]); } </script> </body> </html>
Using LOOP, for the numbers 5 through 35, display: a) every 4th number; b) all numbers less than 12;
<html> <body> <script type="text/javascript"> document.write("<h3>Every 4th number (5-35): </h3>"); for (i=5;i<=35;i=i+4) { document.write("Number: " + i + ";<br>"); } document.write("<h3>All numbers less than 12(5-35): </h3>"); i=5; while (i<12) { document.write("Number: " + i + ";<br>"); i++; } </script> </body> </html>
Write or modify an existing code so page prompts you to input your name when you press the "Try it" button and has a default value of "Harry Potter". After the input is submitted a greeting appears on the page: "Hello <your name>! How are you today?".
<html> <body> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var x; var person=prompt("Please enter your name","Harry Potter"); if (person!=null) { x="Hello " + person + "! How are you today?"; document.getElementById("demo").innerHTML=x; } } </script> </body> </html>
What is the correct syntax for referring to an external script called "xxx.js"?
<script type="text/javascript" src="xxx.js">
Inside which HTML element do we put the JavaScript?
<script>
What is a JavaScript variable?
A container to store data.
What is a JavaScript block?
A group of JavaScript statements logically grouped together surrounded by curly braces {...}.
What is a loop? What kinds of JavaScript loops do we know?
A loop is a cycle that repeats same block of code many times. A for loop is used when you know the exact number (of times) you want to run the block. A while loop continues looping through a block of code while a specified condition is true.
What is Object Oriented Programming?
A type of programming in which programmers define not only the data type of a data structure, but also the types of operations (functions) that can be applied to the data structure. In this way, the data structure becomes an object that includes both data and functions. OOP is a technique that allows a programmer to reuse and simplify the code.
What are the 3 different kinds of pop-up boxes in JavaScript?
Alert box - users verify that information goes through. To proceed click OK. alert("sometext"); Confirm box - users verify or accept something. To proceed click OK (true) or Cancel (false); confirm("sometext"); Prompt box - users are prompted to input a value to continue. To proceed click OK (input value) or Cancel (null); prompt("sometext","defaultvalue");
What built-in JavaScript values do we know?
Array object - stores multiple values in a single variable. Date object - works with dates and times. String object - manipulates a stored piece of text. Math object - performs mathematical tasks. RegExp (Regular Expression) - used to describe a pattern of characters.
How do we control a loop?
By specifying: 1) Start/end condition; 2) Number of cycles.
Where can we place JavaScript in an HTML file?
Either the head or body
What is the syntax for defining functions?
Function with input arguments (values): <script> function myFunction(arg1, arg2, arg3...) { Some code } </script> ...myFunction(val1, val2, va3....) Function with return value: <script> function myFunction() { Some code Return x; } </script> ...myFunction(val1, val2, va3...)
How to read and write a file using JavaScript?
I/O operations like file reading or writing is not possible with client-side JS. But you can code a Java applet that reads files for the script.
What is JavaScript?
JavaScript is a platform-independent,event-driven, interpreted client-side scripting language.
Does the external JavaScript file contain the <script> tag?
No. A JavaScript file contains pure JavaScript, without <script> tags or any other HTML. By convention, files of JavaScript code have names that end with .js.
Name the JavaScript data types
Number, String, Boolean, Function, Object, Null, Undefined.
Where is JavaScript executed?
On the browser side.
What should appear at the very end of your JavaScript?
The </script LANGUAGE="JavaScript"> tag
How would you find today's date in JS code? Just the year only? Time?
Use the Date object. currDate = new Date(); currYear = currDate.getYear()+1900; currTime = currDate.getTime();
How would you find the length of a string?
Use the String object .length var txt="Hello Everybody!"; txt.length will return the number of characters in the string "Hello Everybody!"
How do you write "Hello World" in an alert box?
alert("Hello World")
Can we place function within a function?
Yes. JavaScript function definitions can be nested within other functions, and they have access to any variables that are in scope where they are defined.
Can you pass (a, b, c, d, e, f) into a function as one value? How?
Yes. Use the Array object. var myArray = Array(a,b,c,d,e,f);
How do you write "Hello World" in a confirm box?
confirm("Hello World")
What is the correct JavaScript syntax to write "Hello World"?
document.write("Hello World")
How can you find a client's browser name?
navigator.appName
How would you make "Hello World" the default text for a prompt box?
prompt("Type 'Hello World' here:","Hello World");
How do you display line breaks inside popup boxes?
with backslash n Example: alert("Hello\nHow are you?");
How do you specify a JavaScript block?
{...}