Skip to content
the poor coder

This is JavaScript

Poor Man
Published
Reading time
2 min read
On this page

This is JavaScript#

JavaScript is a high-level programming language that is widely used for creating dynamic and interactive websites. It is a client-side scripting language that runs on the browser side of things and is used to add interactivity and functionality to web pages.

How to use JavaScript in HTML#

To include JavaScript code in an HTML document, you can use the <script> tag like this:

Text
<script>
// JavaScript code goes here
</script>

You can include the JavaScript code within the HTML document using the <script> tag in two ways:

  • Inline JavaScript: This is where the JavaScript code is included directly within an HTML element like this:
Text
<p onclick="alert('Hello, World!')">Click me!</p>
  • External JavaScript: This is where the JavaScript code is saved in a separate file with a .js extension and then linked to the HTML document using the <script> tag like this:
Text
<script src="path/to/your/javascript/file.js"></script>

Using an external JavaScript file is recommended because it allows you to separate your code from your HTML document, making your HTML document cleaner and easier to read.

Basic Syntax of JavaScript#

The basic syntax of JavaScript includes variables, data types, operators, conditional statements, loops, functions, and events. Here is an example of a simple JavaScript program that displays a message on the screen:

Text
var message = "Hello, World!";
alert(message);

The above code declares a variable called message and assigns it the value "Hello, World!". The alert() function then displays the value of the message variable in a pop-up window.

Syntax highlighting with highlight.js#

Highlight.js is a popular open-source library that provides syntax highlighting for various programming languages, including JavaScript. To use highlight.js in your HTML document, you need to include the JavaScript and CSS files in the <head> section of your HTML document like this:

Text
<head>
  <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.2.0/styles/default.min.css">
  <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.2.0/highlight.min.js"></script>
</head>

Once you have included the necessary files, you can use the hljs class to highlight your code like this:

Text
<pre><code class="hljs javascript">
var message = "Hello, World!";
alert(message);
</code></pre>

In the above code, the hljs class is used to highlight the code within the <pre> and <code> tags. The javascript class is used to specify the language being highlighted.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC