Category Archives: Javascript

Document JavaScript

The document comes under the windows object and can also be considered as its property.

JavaScript Document object is an object that provides access to all HTML elements of a document. When an HTML document is loaded into a browser window, then it becomes a document object. The document object stores the elements of an HTML document, such as HTML, HEAD, BODY, and other HTML tags as objects.

Imports And Exports JavaScript

Imports and exports help in writing modular code for our JavaScript applications. With the help of imports and exports, we can split a JavaScript code into multiple files in a project. This greatly simplifies the application source code and encourages code readability.

Therefore, here we import those functions and pass input to those functions to calculate square and diagonal.

Defining Variable JavaScript

There are three ways of defining a variable in JavaScript:

Var
This is used to declare a variable and the value can be changed at a later time within the JavaScript code.

Const
We can also use this to declare/define a variable but the value, as the name implies, is constant throughout the JavaScript program and cannot be modified at a later time.

Let
This mostly implies that the values can be changed at a later time within the JavaScript code.

Access HTML Element JavaScript

Here are the ways an HTML element can be accessed in a JavaScript code:

  • getElementByClass(‘classname’): Gets all the HTML elements that have the specified classname.
  • getElementById(‘idname’): Gets an HTML element by its ID name.
  • getElementbyTagName(‘tagname’): Gets all the HTML elements that have the specified tagname.
  • querySelector(): Takes CSS style selector and returns the first selected HTML element.

Delete Cookie JavaScript

To delete a cookie, we can just set an expiration date and time. Specifying the correct path of the cookie that we want to delete is a good practice since some browsers won’t allow the deletion of cookies unless there is a clear path that tells which cookie to delete from the user’s machine.

function delete_cookie(name) {

document.cookie = name + “=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;”;

}

Read Cookie JavaScript

Reading a cookie using JavaScript is also very simple. We can use the document.cookie string that contains the cookies that we just created using that string.

The document.cookie string keeps a list of name-value pairs separated by semicolons, where ‘name’ is the name of the cookie, and ‘value’ is its value. We can also use the split() method to break the cookie value into keys and values.

JavaScript Advantages

These are the advantages of JavaScript:

Enhanced Interaction
JavaScript adds interaction to otherwise static web pages and makes them react to users’ inputs.

Quick Feedback
There is no need for a web page to reload when running JavaScript. For example, form input validation.

Rich User Interface
JavaScript helps in making the UI of web applications look and feel much better.

Frameworks
JavaScript has countless frameworks and libraries that are extensively used for developing web applications and games of all kinds.