Home / Programming / JavaScript Interview Questions and Answers
JavaScript

JavaScript Interview Questions and Answers

What is JavaScript?

JavaScript is a programming language used to develop static and dynamic websites. It is used with HTML and CSS. This means you can change the HTML and CSS of a website with JavaScript. It can also change the behavior of a webpage. To use the complete functionality of a website, you have to enable JavaScript in your power. From this, you can know the importance of JavaScript on a website.

JavaScript Implementation

Implementing JavaScript into your website or webpage is very easy. You can add JavaScript into your webpage by three steps.

  • Inline Code
  • Embedding Code
  • External File

Inline Code

Inline code is a way to implement programming languages like CSS, JavaScript into the main language like HTML. This means if you want to add a JavaScript code to your webpage you can add by Inline code method. Let consider what the Inline Code method is?

Imagine if you have a webpage programmed with HTML and CSS and you want to add JavaScript in it to make it more functional. You have an HTML tag like <button>This is HTML</button>. You can add JavaScript only to this button tag.

<!DOCTYPE html>
<html>
<body>
<h2>My First JavaScript</h2>
<button type="button"
onclick="document.getElementById('demo').innerHTML = Date()">
Click me to display Date and Time.</button>
<p id="demo"></p>
</body>
</html>

You can see the code above that, I have only added JavaScript code only into the button tag. This button will display the date when it will be clicked. In this way, you can add JavaScript only to the tag in which you want to add not affecting other tags or elements.

Embedding Code

Embedding code is another way to add JavaScript code into an HTML document. With embedding code, you can add JavaScript code an element with the script tag like:

<!DOCTYPE html>
<html>
<body>
</p id="demo>JavaScript Implementation</p>
<script>
document.getElementById("demo").innerHTML = "My First JavaScript";
</script>
</body>
</html> 

You can see, I have only added the Javascript only in the p tag and it will only affect on p tag, not on other tags. In this way, you can embed JavaScript to CSS code into an HTML document.

External File

The external file is different form both methods above. In the external file method, you have to create a new javascript file with .js extension and link it with your HTML document.

<!DOCTYPE html>
<html>
<head>
<script src="Your file path" type="text/javascript"></script>
</head>
body>
</p id="demo>JavaScript Implementation</p>
</body>
</html> 

Please keep that thing in mind you have to link your JavaScript file in the head section of your HTML document. In the src just add your file location where you stored your file in your PC. You can also add a website URL in src.

Leave a Reply

Your email address will not be published. Required fields are marked *