Flashcards
Q1: What are the three types of JavaScript code placement?
A1: Inline, Internal, External.
Q2: What is a primary advantage of external JavaScript files?
A2: Code reusability across multiple HTML files.
Q3: What is a disadvantage of using external JavaScript files?
A3: Vulnerability to code theft and dependency on external files.
Simple Explanations with Examples
Inline JavaScript
Explanation: Inline JavaScript is directly written inside an HTML element's
attribute, such as onclick. It is useful for small, specific tasks but can clutter the
HTML file.
Example:
html
Copy code
<button onclick="alert('Hello World')">Click Me</button>
Internal JavaScript
Explanation: Internal JavaScript is placed within a <script> tag in the HTML file.
It's useful for tasks specific to a single HTML page.
Example:
html
Copy code
<!DOCTYPE html>
<html>
<head>
<script>
function greet() {
alert('Welcome to Internal JavaScript');
}
</script>
</head>
<body>
<button onclick="greet()">Greet</button>
</body>
</html>
External JavaScript
Explanation: External JavaScript is stored in a .js file, linked to the HTML file using
a <script> tag. This method allows reusability across multiple pages.
Example:
HTML:
html
Copy code
<!DOCTYPE html>
<html>
<head>
<script src="script.js"></script>
</head>
<body>
<button onclick="greet()">Click Me</button>
</body>
</html>
script.js:
javascript
Copy code
function greet() {
alert('This is External JavaScript');
}
Key Points to Remember
1. Inline JavaScript:
o Best for quick, simple commands.
o Increases HTML file size and decreases maintainability.
2. Internal JavaScript:
o Ideal for page-specific scripts.
o Combines script and HTML in a single file.
3. External JavaScript:
o Promotes code reusability.
o Requires careful handling to avoid errors due to missing files.
Key Terms and Definitions
1. Inline JavaScript:
o Definition: JavaScript embedded directly in HTML element attributes.
o Example: <button onclick="alert('Hello')">Click Me</button>.
2. Internal JavaScript:
o Definition: JavaScript written within a <script> tag in the HTML file.
o Example: A function inside a <script> tag.
3. External JavaScript:
o Definition: JavaScript code in an external .js file linked to an HTML
document.
o Example: <script src="script.js"></script>.
4. Script Tag:
o Definition: HTML tag used to include JavaScript in a web page.
o Example: <script>.
Quiz Questions
Q1: Which JavaScript placement method is ideal for reusing the same script across
multiple HTML files?
a) Inline
b) Internal
c) External
d) None of the above
Answer: c) External
Q2: What is the primary disadvantage of using inline JavaScript?
a) Increases maintainability.
b) Encourages reusability.
c) Makes HTML cluttered.
d) Easier to debug.
Answer: c) Makes HTML cluttered.
Q3: Where is internal JavaScript written?
a) Inside an HTML element attribute.
b) Within a <script> tag in the HTML file.
c) In a separate .js file.
d) None of the above.
Answer: b) Within a <script> tag in the HTML file.
Q4: What is a potential risk of using external JavaScript files?
a) Improved flexibility.
b) Code theft.
c) Easier debugging.
d) Improved performance.
Answer: b) Code theft.
Q5: What file extension is typically used for external JavaScript files?
a) .css
b) .html
c) .js
d) .txt
Answer: c) .js