JavaScript?
Java Script is a dynamic programming language that’s used for web development, in web applications, for game development, and lots more. It allows you to implement dynamic features on web pages that cannot be done with only HTML and CSS.
Who uses JavaScript?
Moreover, many browsers use JavaScript as a scripting language for doing dynamic things on the web. Any time you see a click-to-show dropdown menu, extra content added to a page, and dynamically changing element colors on a page, to name a few features, you’re seeing the effects of JavaScript.
What are the uses of JavaScript?
JavaScript is a light-weight object-oriented programming language that is used by several websites for scripting the webpages. It is an interpreted, full-fledged programming language. Java Script enables dynamic interactivity on websites when it is apply to an HTML document.
Further, JavaScript helps the users to build modern web applications to interact directly without reloading the page every time. Moreover, JavaScript is commonly use to dynamically modify HTML and CSS to update a user interface by the DOM API. It is mainly use in web applications.
There are some uses of Java Script are here:
- Web Development
- Mobile Applications
- Presentations
- Game
- Server Applications
- Web Servers
- Web Applications
How to add JavaScript to html?
JavaScript, also known as JS, is one of the scripting (client-side scripting) languages, that is usually used in web development to create modern and interactive web-pages. But the term “script” is used to refer to the languages that are not standalone in nature and here it refers to JavaScript which run on the client machine.
In other words, we can say that the term scripting is used for languages that require the support of another language to get executed. For example, JavaScript programs cannot executed without the help of HTML
or without integrated into HTML code.
Just like with CSS, JavaScript can be use in HTML in various ways, such as:
1. Inline JavaScript
Here, you have the JavaScript code in HTML tags in some special JS-based attributes.
For example, HTML tags have event attribute that allow you to execute some code inline when an event is trigger. Here’s what I mean:
<button on click=”alert (‘You just clicked a button’)”>Click me! </button>
Moreover this is an example of inline JavaScript. The value of on click can be some Match calculation, a dynamic addition to the DOM – any syntax-valid JavaScript code.
2. Internal JavaScript, with the script tag
Just like the style tag for style declarations within an HTML page, the script tag exists for JavaScript. Here’s how it’s use:
<script>
function () {
alert (“I am inside a script tag”)
}
</script>
3. External JavaScript
Further, you may want to have your JavaScript code in a different file. Then external JavaScript allows this. For such uses-cases, here’s how it’s done:
<! — index.html –>
<script src=”./script.js”></script>
// script.js
alert (“I am inside an external file”);
Moreover, the src attribute of the script tag allows you to apply a source for the Java Script code. That reference is important because it notifies the browser to also fetch the content of script.js.
script.js can be in the same directory with index.html, or it can be gotten from another website. For the latter, you’ll need to pass the full URL (https://…/script.js).
Functions in JavaScript?
Moreover, some functions, you can store a block of code that can be used in other places in your code. Say you wanted to print “JavaScript” and “Language” at different places in your code. Instead of doing this:
console.log(“JavaScript”)
console.log(“Language”)
// some things here
console.log(“JavaScript”)
console.log(“Language”)
// more things here
console.log(“JavaScript”)
console.log(“Language”)
Then you can do this:
function print () {
console.log(“JavaScript”)
console.log(“Language”)
}
print ()
// some things here
print ()
// more things here
print ()
Although, this way, we’ve stored the repeated code block in a function that can be use wherever we want. But that’s not all. Say we wanted to find the average of three numbers. The code for this would be:
let num1 = 5
then num2 = 6
then num3 = 8
let average = (num1 + num2 + num3) / 3
Doing this outside of a function may not hurt, but if we had to do that in many places? Then, we’d have a function like so:
function find Average(n1, n2, n3) {
let aver = (n1 + n2 + n3) / 3
return avers
}
let num1 = 5
then num2 = 6
then num3 = 8
let average = find Average(num1, num2, num3)
// later on, somewhere else
let average2 = find Average(…)
// later on, somewhere else
let average3 = find Average(…)
Moreover, as you’ll notice in find Average’s declaration, we have n1, n2, n3 in the parentheses. These are parameters, which serve as placeholders for values that would be provided when the function is to be called.
Conclusion
However ,JavaScript has many more features we could discuss, but I hope this article has given you a clear starting point to go further. Now you should know what the language is and how you can use it on the web.
For more informative article visit this link: