18 Questions
Which keyword is used to declare variables in JavaScript?
var
What method is used to find the largest value between two numbers in JavaScript?
max
Which of the following data types is not valid in JavaScript?
Double
What is the result of '5 + 4 + (2 * 3)' in JavaScript?
15
What comment syntax is used in JavaScript similar to Java?
// comment
If a variable has not been declared and does not exist, what value does it hold in JavaScript?
'undefined'
What does document.getElementById return in JavaScript?
The DOM object for an element with a given id
How can you change the text inside most elements in the DOM using JavaScript?
By setting the innerHTML property
In JavaScript, what property can be set to change the text in form controls like textboxes?
value
What does the function changeText() in JavaScript do?
Change the style of a textbox
Which method allows you to manipulate or initialize text in JavaScript?
changeText()
Which CSS style property is NOT changed in the function Preetify() in JavaScript?
background color
What type of programming is JavaScript known for?
Event-driven programming
In JavaScript, what is used to write a JavaScript function that will run when an event occurs?
JavaScript event handler
What is an example of an event attribute commonly used in HTML?
onchange
How can a JavaScript function be invoked in response to a user's click on an element?
onclick
attribute
What is a common drawback associated with using alert windows in JavaScript?
They disrupt the user experience
Which HTML attribute is used to set a JavaScript function as an event handler for element interactions?
onclick
Study Notes
Variables
- In JavaScript, variables are declared using the
var
keyword. - Variable types are not specified, but JavaScript has types including Number, Boolean, String, Array, Object, Function, Null, and Undefined.
- The
typeof
operator can be used to find out a variable's type.
Number Type
- Integers and real numbers are the same type in JavaScript.
- Same operators are used for numbers:
+
,-
,*
,/
,%
,++
,--
,=
,+=
,-=
,*=
,/=
,%=
. - Many operators auto-convert types, e.g.,
"2" * 3
is 6.
Comments
- JavaScript comments are the same as Java comments.
- There are four comment syntaxes: HTML, CSS/JS/PHP, Java/JS/PHP, and PHP.
Math Object
- The Math object has methods:
abs
,ceil
,cos
,floor
,log
,max
,min
,pow
,random
,round
,sin
,sqrt
,tan
. - The Math object has properties:
E
andPI
. - Examples:
var rand1to10 = Math.floor(Math.random() * 10 + 1);
,var three = Math.floor(Math.PI);
.
Null and Undefined
-
null
exists, but was specifically assigned an empty or null value. -
undefined
has not been declared, does not exist.
Document Object Model (DOM)
- Most JavaScript code manipulates elements on an HTML page.
- We can examine elements' state and change state.
- Examples: see whether a box is checked, insert new text into a div, change styles.
Accessing Elements
-
document.getElementById
returns the DOM object for an element with a given id. - We can change the text inside most elements by setting the
innerHTML
property. - We can change the text in form controls by setting the
value
property.
Functions
- A function is a block of code that can be called multiple times from different parts of the script.
- Example:
function changeText() { ... }
. - Functions can be used as event handlers.
Event-Driven Programming
- JavaScript programs wait for user actions called events and respond to them.
- This is called event-driven programming.
- Example: a clickable button that pops up a "Hello, World" window.
Test your knowledge on JavaScript variables and types syntax by answering questions related to declaring variables, assigning values, and understanding data types in JavaScript. Explore concepts like loosely typed nature, types such as Number, Boolean, String, Array, Object, Function, Null, and Undefined, and identifying variable types using 'typeof'.
Make Your Own Quizzes and Flashcards
Convert your notes into interactive study material.
Get started for free