All posts by Pramod T P

Defining Variable JavaScript

There are three ways of defining a variable in JavaScript:

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

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.

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.

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