Open In App

JavaScript Course Understanding Code Structure in JavaScript

Inserting JavaScript into a webpage is much like inserting any other HTML content. The tags used to add JavaScript in HTML are <script> and </script>. The code surrounded by the <script> and </script> tags is called a script blog. The ‘type‘ attribute was the most important attribute of <script> tag. However, it is no longer used. The browser understands that <script> tag has JavaScript code inside it.

<script>
// JavaScript Code
</script>

Write Structured JavaScript Code: To write a structured JavaScript code right now you have to careful about Statements, Semicolons, and Comments.



JavaScript can be added to your HTML file in two ways:

Method 1(Internal JavaScript):



Method 2(External JavaScript):

<script src='relative_path_to_file/file_name.js'></script>

 

 

Let’s understand the code structure with the help of a simple JavaScript example that will make a block disappear with javascript only.

Code Structure: 

Example: Here in this example we have created HTML, CSS and JavaScript file to show the actual directory structure with valid code structure.




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="script.js"></script>
    <link rel="stylesheet" href="styles.css">
    <title>Button Vanisher</title>
</head>
  
<body>
    <a onclick="toggle('plain')">
        <div id="plain">
          How many times were you frustrated while looking out for a 
          good collection of programming/algorithm/interview questions? 
          What did you expect and what did you get? This portal has 
          been created to provide well written, well thought and 
          well-explained solutions for selected questions.
          Geeksforgeeks is the one stop solution to all 
          your coding problems! Join us so that we can shape 
          your future.
        </div>
    </a>
</body>
  
</html>




#plain {
    border: 2px solid black;
    max-width: 200px;
    height: 300px;
    margin: 0 auto;
    display: block;
}
  
a {
    display: block;
}




// JavaScript function to change the content of the 
  
function toggle(id) {
    let button = document.getElementById(id);
    if (button.style.display == 'block') {
        button.style.display = 'none';
    } else {
        button.style.display = 'block';
    }
}

Output: Live Output

 

Code Explanation:

The above code is a decent example of how we should make a directory, how to link different types of code files with each other, and how to write simple yet effective code.


Article Tags :