View Discussion Show Improve Article Save Article View Discussion Improve Article Save Article Previous article:
JavaScript Course | Printing Hello World in JavaScript <script></script> How to write, run and save code Method 1:
Method 2:
<script src='relative_path_to_file/file_name.js'></script>
Let’s understand the code structure with the help of a simple javascript example where will make a block disappear with javascript only. index.html styles.css script.js index.html html
The above HTML code contains a simple div element which is wrapped inside an anchor tag(link) so that whenever we click the div element, the javascript code works. Inside the ‘div’ element there’s some random text. Inside the script tag we are linking the javascript file saved as ‘script.js’ with the HTML document. Also the CSS file is
linked too. css
The
above CSS code only targets two element ‘a’ and ‘div’ element whose ID is ‘plain’. javascript
The above Javascript code is called when we
click the ‘div’ button, as we have linked them with the ‘a’ tag and also ‘onclick=’toggle(plain)’. Inside the function we are passing the ‘ID’ of the ‘div’ element and then accessing the element from the ‘DOM’ using the getElementByID method and then a simple if-else condition which checks if the ‘div’ is of type ‘block’ then change the display to none, else change it to display block. Output: (After Clicking anywhere on div) Learning from the Code
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. Supported Browser:
Next article: JavaScript Course | Variables in JavaScript How is JavaScript structured?JavaScript programs consist of a series of instructions known as statements, just as written paragraphs consist of a series of sentences. While a sentence will end with a period, a JavaScript statement often ends in a semicolon ( ; ).
How do I structure a JavaScript project?The 5 fundamental rules of a Node.. Rule 1 – Organize your Files Around Features, Not Roles. ... . Rule 2 – Don't Put Logic in index.js Files. ... . Rule 3 – Place Your Test Files Next to The Implementation. ... . Rule 4 – Use a config Directory. ... . Rule 5 – Put Your Long npm.. How do you structure your code?Keeping classes and functions as small as possible helps to make code easier to understand. As a rule of thumb split larger classes and function into smaller specialized ones. Follow the Single Responsibility Principle which means every class and function should do one thing.
Can I use Notepad ++ for JavaScript?If you are a beginner and want to go for a simple approach for writing JavaScript programs, use a simple text editor like Notepad++. It is free, open-source, and will work fine for JavaScript development. Notepad++ is a general-purpose editor that supports highlights the syntax of JavaScript and HTML code.
|