Chapter 8 : Introduction to JavaScript Flashcards
Who invented JavaScript?
- Brendan Eich ( 1995 )
- Java
- General Purpose Programming Language
JavaScript - Used on websites to make them animated and interactive
- General Purpose Programming Language
List out 5 examples that JavaScript can do?
- Change HTML content
- Change HTML attributes
- Change HTML styles ( CSS )
- Hide HTML elements
- Show HTML elements
What HTML tag is used for JavaScript , CSS?
<script> </script>
- <style></style>
When does the JavaScript function called?
- Executed when the JavaScript function called
- A function can be called when an event occurs, like when the user clicks a button
What is the file extension for JavaScript files?
- .js
How to link external JavaScript ?
- Can be placed inside < head > or < body >
What is the advantages of JavaScript? ( 3 )
- Separates HTML and code
- Makes HTML and JavaScript easier to read and maintain
- Cached JavaScript files can speed up page loads
How to add several script files?
<scri[t src=”myScript1.js”>
</script>
List out 4 ways that JavaScript can display data
- window.alert() - Alert Box
- document.write() - Write into HTML Output
- innerHTML - Write into HTML element
- console.log() - Write into Browser Console
How to show window alert of 5 + 6 ?
<!DOCTYPE html>
<html>
<body>
<script> window.alert(5+6); </script>
</body>
</html>
How to use document.write() to show 5 + 6 ?
<!DOCTYPE html>
<html>
<body>
<button>Try It</button>
</body>
</html>
- Write the answer 11 where the code is placed
How to display new paragraph for 5 + 6 using JavaScript?
<!DOCTYPE html>
<html>
<body>
<p></p>
<script> **document.getElementById("demo").innerHTML = "<h2>5 + 6</h2>";** </script>
</body>
</html>
How to write the total amount for 5 + 6 into browser console?
<!DOCTYPE html>
<html>
<body>
<script> console.log(5+6) <script> </body> </html> </script></body></html>
What is JavaScript Syntax?
- JavaScript syntax is the set of rules of how JavaScript programs are constructed
List out 5 statements that JavaScript are composed of
- Values
- Operators
- Expressions
- Keywords
- Comments
- Statements are being executed one by one, in the same order as they are written
What are program instructions called?
- Statements
What is JavaScript statement seperated by?
- Semicolons
List out JavaScript Arithmetic Operators ( 7 )
- /
- %
- ++
- –
List out JavaScript assignment operator ( 6 )
- =
- +=
- -=
- *= ( x = x * y )
- /=
- %=
List out JavaScript Comparison Operators ( 9 )
- ==
- === ( Equal value and equal type )
- !=
- !== ( Not equal value and not equal type )
- >
- <
- > =
- <=
- ?
List out 2 JavaScript Type Operators
- typeof
- Returns the type of a variable
- instanceof
- Returns true if an object is an instance of an object type
What are JavaScript comments being used to? ( 2 )
- Explain JavaScript code, and to make it more readable
- Prevent execution, when testing alternative code
List out :
1. Single Line Comments
2. Multiline Comments
- //
- Starts with /* , End with */
What are variables used for?
- Containers for storing data values
What does all JavaScript variables must be identified with?
- Unique names
- It is called Identifiers
- Can be short names ( x , y ) or more descriptive names ( age, sum, totalVolume )
List out general rules for construction names for variables ( 6 )
- Names can contain letters, digits, underscores, and dollar sign
- Names must begin with a letter
- Names can also begin with $ and _
- Names are case sensitive
- Reserved words ( JS Keywords ) cannot be used as names
- JavaScript identifiers are case-sensitive
What keyword can we use to declare JavaScript variable? ( 3 )
- var
- let
- const
- let and const are added in 2015
How to create a variable using the var keyword?
- var carName
- The variable has no value ( Technically it has the value of undefined )
carName = “Volvo” to assign data with the assignment operator
var carName = “Volvo” assign a variable when we declare it
List out 4 dataTypes that the var keyword can handle
- String
- Numbers
- Array
- Object
- And more
How to assign string John Doe to var userName?
- var userName = “John Doe”;
- Can use “” or ‘’ ( Double / Single Quotes )
- var ans = “He is called ‘Johnny’” / ‘He is called “Johnny”’
- We cannot type “He is called “Johnny””
How to assign value 30 to variable age?
- var age = 30;
- Decimals
var price = 29.99 - Exponential
var y = 123e5 ( 12300000 )
var z = 123e-5 ( 0.00123 )
What is the dataType for user input?
- String
What method is available to convert user input string value into number? ( 3 )
- Number()
- Converts an object’s value to a number
- parseFloat()
- Parses a string and returns a floating point number
- parseInt()
- Parses a string and returns an integer
Number(“10”);
parseInt(“-10”);
parseInt(“10 20”);
parseInt(“10 Years”);
What symbols are used to write Arrays?
- Square Brackets []
How to declare a variable cars to store 3 values Saab, Volvo and BMW?
- var cars = [“SAAB” , “Volvo” , “BMW”]
- Array items are seperated by commas
What symbols are used to write JavaScript objects?
- Curly braces {}
How to create an objects that stores user first / last name, age and eye color?
- var person = { firstName : “John” , lastName : “Doe” , age : 50, eyeColor : “Blue”}