1. About JavaScript code, examples

5/5 - (1 vote)

JavaScript is an interpreted, object-oriented language and is known as the scripting language for web pages. One of the big positive points of JavaScript is its popularity. This is also because JavaScript can be used for a very wide range of applications. It can be used for front-end, back-end, or even some mobile device applications.

JavaScript Language Features

The most important and specific things to keep in mind about JavaScript are the following:

– Is interpreted (not compiled)

– Dynamically and Weekly typed (“hello world” + x, is permitted)

– Is Single threated

– Is Object-Oriented, but with a few specifications (for example it does not allow private variables)

An important benefit is that the JavaScript code can be executed on the client-side, which means that will reduce the load on the server and the applications will run fast.

How to add javascript code in HTML?

JavaScript uses the Unicode character set. Unicode covers almost all the characters, punctuations or symbols. In HTML pages, JavaScript code must be placed between <script> and </script> tags.

<script>
alert("Hi webpedia");
</script>

There are old javascript examples that use “type attribute” inside <script> tag like:

<script type="text/javascript">
alert("Hi webpedia");
</script>

The type attribute is not required because JavaScript is the default scripting language in HTML.

Where to place JavaScript code in HTML?

The JavaScript code can be placed inside the <body> or in the <head> section of any HTML page.
To improve page load, is recommended to place the JavaScript code at the bottom of the <body> element, just before </body> tag.

The position of JavaScript code in a page does matter.
By default the code will be executed as soon as the browser sees it.

External JavaScript code

JavaScript code can also be placed in external files. This method brings us some advantages:

  • Use the same code in many different web pages.
  • Separate HTML and JavaScript code.
  • Cache the JavaScript files, which can improve the site speed.
  • Also, the JavaScript code is easier to read.

JavaScript files have the file extension.js and must be placed in <body> or <head>, inside the src (source) attribute of the <script> tags like this:

<script src="myFile.js"></script>

Make sure the external .js files does not contain <script> tags!!!

Hello there!

I hope you find this post useful!

I'm Mihai, a programmer and online marketing specialist, very passionate about everything that means online marketing, focused on eCommerce.

If you have a collaboration proposal or need helps with your projects feel free to contact me. I will always be glad to help you!

subscribe youtube

Leave a Comment

WebPedia.net