Trending March 2024 # Practical Code Examples Using Javascript # Suggested April 2024 # Top 7 Popular

You are reading the article Practical Code Examples Using Javascript updated in March 2024 on the website We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested April 2024 Practical Code Examples Using Javascript

Example#1: JavaScript Multiplication Table

Create a simple multiplication table asking the user the number of rows and columns he wants.


var rows = prompt(“How many rows for your multiplication table?”); var cols = prompt(“How many columns for your multiplication table?”); rows = 10; cols = 10; createTable(rows, cols); function createTable(rows, cols) { var j=1; for(i=1;i<=rows;i++) { while(j<=cols) { j = j+1; } j = 1; } document.write(output); }

Example#2: JS Forms Example:

Create a sample form program that collects the first name, last name, email, user id, password and confirms password from the user. All the inputs are mandatory and email address entered should be in correct format. Also, the values entered in the password and confirm password textboxes should be the same. After validating using JavaScript, In output display proper error messages in red color just next to the textbox where there is an error.

Solution with Source Code:

var divs = new Array(); divs[0] = “errFirst”; divs[1] = “errLast”; divs[2] = “errEmail”; divs[3] = “errUid”; divs[4] = “errPassword”; divs[5] = “errConfirm”; function validate() { var inputs = new Array(); inputs[0] = document.getElementById(‘first’).value; inputs[1] = document.getElementById(‘last’).value; inputs[2] = document.getElementById(’email’).value; inputs[3] = document.getElementById(‘uid’).value; inputs[4] = document.getElementById(‘password’).value; inputs[5] = document.getElementById(‘confirm’).value; var errors = new Array(); for (i in inputs) { var errMessage = errors[i]; var div = divs[i]; if (inputs[i] == “”) document.getElementById(div).innerHTML = errMessage; else if (i==2) { var atpos=inputs[i].indexOf(“@”); var dotpos=inputs[i].lastIndexOf(“.”); else document.getElementById(div).innerHTML = “OK!”; } else if (i==5) { var first = document.getElementById(‘password’).value; var second = document.getElementById(‘confirm’).value; if (second != first) else document.getElementById(div).innerHTML = “OK!”; } else document.getElementById(div).innerHTML = “OK!”; } } function finalValidate() { var count = 0; for(i=0;i<6;i++) { var div = divs[i]; if(document.getElementById(div).innerHTML == “OK!”) count = count + 1; } if(count == 6) document.getElementById(“errFinal”).innerHTML = “All the data you entered is correct!!!”; } Example#3: POPUP Message using Event:

Display a simple message “Welcome!!!” on your demo webpage and when the user hovers over the message, a popup should be displayed with a message “Welcome to my WebPage!!!”.


function trigger()


document.getElementById(“hover”).addEventListener(“mouseover”, popup);

function popup()


alert(“Welcome to my WebPage!!!”);






position: fixed;

left: 550px;

top: 300px;


You're reading Practical Code Examples Using Javascript

Notion Buttons – Tutorial With 4 Practical Examples

Notion is the best app for organizing your life. Personally, I use Notion for writing texts, planning my tasks and projects, as well as storing notes.

The unique selling point of Notion is its innovative combination of blocks and databases. However, one small hurdle so far has been the somewhat cumbersome process of making changes in the databases.

Fortunately, this problem has disappeared with one of the recent Notion updates – the introduction of Notion Buttons in March 2023.

In this post, I want to show you what this update means, how to create Notion Buttons and the use cases where you can employ these buttons.

As hinted earlier, with Notion Buttons, you can efficiently perform frequently used actions or insert content, and they can be inserted on any Notion page.

They can be used to:

Insert blocks on a page.

Add pages to a database.

Edit pages in a database.

Open specific pages in your Notion workspace.

Type “/” and search for “Button”.

Now you can label the button and even add an emoji.

Here, you can add the following steps:

Insert Blocks: Insert a Notion block above or below the button. In theory, you can insert any possible Notion block, even another Notion button.

Add Pages to: This will add a page to a database. Simply select the appropriate database and choose the properties that should be filled.

Edit Pages in: Edit pages in a database, so that you can change the date or status, for example. You can also use filters to specify that only pages with specific properties should be modified.

Show Confirmation: This option is useful when you want the user to confirm an action. It can be used, for example, when redirecting them to an external page in the next step.

Open Page: This opens an external page.

Now that you know how to create a Notion button, let’s explore what you can actually do with it. Below, I provide you with 4 specific application examples from my Notion templates:

AI Summarization of Notes in a Zettelkasten

I find this function very important for my Notion Zettelkasten, as it allows me to quickly create readable text from a series of unformatted notes.

Show or Hide All Answers in a Spaced Repetition Template

The Spaced Repetition method is one of the best techniques for reliably memorizing a large amount of information. My Spaced Repetition template now brings this method closer to the Notion community.

To achieve this, I selected the step “Edit Pages” and linked it to the Spaced Repetition database. To hide the answers, I chose the “Reveal Answers” property and instructed the checkbox to remain unchecked everywhere. To show the answers, I created another button where the checkbox is marked as active.

Start or End a Pomodoro Interval

The Pomodoro method is a time management technique developed by Francesco Cirillo in the late 1980s. It divides work into intervals, typically 25 minutes long, to ensure regular breaks for regeneration.

Francesco Cirillo used to measure time with his Pomodoro kitchen timer, hence the name Pomodoro.

My Notion template allows you to implement the Pomodoro technique directly in Notion and connect your work tasks with your Notion task manager.

The only drawback: The template does not include a Pomodoro kitchen timer :(.

With the help of a Notion button, you can start a new Pomodoro interval or end an ongoing interval. The database will track the duration of the interval, allowing you to monitor your work time.

To start a new Pomodoro interval, the Notion button adds a new page to the Pomodoro database. The start time is set to “Now,” and the status is set to “Active.”

The button to end the Pomodoro interval updates the database and all active pages. The end time is set to “Now,” and the status is changed to “Done.”

Adding New Habits and Marking them as Completed

In my Habit Compass template, I use Notion buttons to add new habits for the day and check them off.

Users of the template are invited to customize the button. For example, they can change the habit’s name and indicate which vision from the vision board it should be associated with.

“Check Habit” modifies database entries and marks the respective habit as completed.

In my tutorial, I showed you how I use Notion buttons in my workspace. However, I’m also curious about your experiences. How do you use Notion buttons in your work routine? Do you have any useful tips or tricks you’d like to share?

I look forward to hearing from you!

Yours, Philipp

Google Recommends Using Javascript “Responsibly”

Google’s Martin Splitt, a webmaster trends analyst, recommends reducing reliance on JavaScript in order to provide the best experience for users.

In addition, “responsible” use of JavaScript can also help ensure that a site’s content is not lagging behind in Google’s search index.

These points were brought up during the latest SEO Mythbusting video which focuses on web performance.

Joined by Ada Rose Cannon of Samsung, Splitt discussed a number of topics about web performance as it relates to SEO.

The discussion naturally led to the topic of JavaScript, as overuse of JS can seriously drag down the performance of a website.

Here are some highlights from the discussion.

JavaScript sites may be lagging behind

Overuse of JavaScript can be especially detrimental to sites that publish fresh content on a daily basis.

As a result of Google’s two-pass indexing process, fresh content on a JS-heavy site may not be indexed in search results for up to a week after it has been published.

When crawling a JS-heavy web page, Googlebot will first render the non-JS elements like HTML and CSS.

The page then gets put into a queue and Googlebot will render and index the rest of the content when more resources are available.

Use dynamic rendering to avoid a delay in indexing

One way to get around the problem of indexing lag, other than using hybrid rendering or server-side rendering, is to utilize dynamic rendering.

Dynamic rendering provides Googlebot with a static rendered version of a page, which will help it get indexed faster.

Rely mostly on HTML and CSS, if possible

When it comes to crawling, indexing, and overall user experience its best to rely primarily on HTML and CSS.

Splitt says HTML and CSS are more “resilient” than JavaScript because they degrade more gracefully.

For further information, see the full video below:

Keras Input Code Using Sequential Api

Definition of Keras input

Keras input is part of the Tensorflow library which is mostly used for providing a Keras object related to tensor. This tensor object related to Keras can be used for augmenting certain attributes with objects for manipulation. These attributes are then used within the object for building and designing Keras model accordingly. In this model building continuous feedback with a set of input and set of output is required mandatorily. Keras input also deals with the layers of the model according to the requirement which makes designing and other fabrication of the model with proper fitting within it.

Start Your Free Data Science Course

Keras input Explanation

Keras input is used for providing proper input with proper output.

In Keras, the input layer plays a significant role as it gives some information to the model or the image.

Each of the keras layers has some useful information that needs a proper channel for transferring information.

In keras layers, there are certain units that represent each of the neurons in it and then each of the layers are associated with certain network topologies.

There are tuples representing elements in an array or tensor flow in dimensions.

Input shapes are the emphasizing channel for any of the input layer as it helps in providing proper data and shapes for input to make use of data training to compute layers automatically.

With shapes another important attribute for keras input is the keras dimensions associated with each layer which are as follows:

– 1D convolution layer

These keras input to the layers can be anything like dimensions, weights, input shapes, units anything. The input layer is the starting layer or the initial layer from where the entire flow of data with its subsequent layers happens till the end layer.

Although the keras layer is the starting point or initiation of any flow it just takes the parameter but is not an actual layer to directly interact with another set of layers for making input parameters reach their actual point of contact includes a hidden layer definition in between where the connectivity between starting and connecting point happens in this way.

2D convolution or 1D convolution layer also makes some of the traversings in another way through the hidden layer consecutively.

Keras input Code

Since keras input refers to a combination of many layers and is the starting point of any layer flow and model training which can be either performed using Sequential API or by using functional API. It is always recommended to make use of the functional API() type for such implementation as it helps in defining functions with some customs whereas sequential API takes some complex parameters which become a little difficult to handle the flow or processing of the data at each layer. Both the ways considering sequential API and functional API with parameters are represented below:

Keras input code using Sequential API

Keras input code using Functional API

Functional API is used for designing a model where the model gets in certain input using input tensor and then that input is provided with some of the hidden layers which comprise of other hidden layers with input to provide the respective output. Here the definition is made in a model with start and endpoint.

At least the model provides attributes with these parameters are provided to model.

Keras input shape

The input function is used to instantiate a Keras tensor which basically includes attributes that allow us to provide certain functionalities to the model. It is a tensor-type object that allows making instances with the model just by knowing its input and respective output.

For example, if the implementation requires input as [a,b] and output requires value as [c] then, in that case, it is required to make the inputs with a set of consecutive output.

Arguments involve the following parameters for any functioning which are as follows:

All these arguments come into the picture only after the input layer is defined with the shape input.

Input shape in Keras or input layer is the tuple which is a set of integers that don’t include any of the batch sizes. To get a clear understanding let’s take an example:

I am sending an input of shape (2×2440) which has 2 rows and 2440 columns which means that the column represents features for it. There are in total nearly 70000 vectors in it. But let’s suppose a query is made on it which gives some response as (?, 2440) then what actually it means is that the model is expecting an arbitrary number of 2×2440 vectors which generally don’t want to fix the number of inputs to the model of a certain number.

If the vector defined gives the value as none then in that case it is representing that the input shape provided is not considering the elements it means that it is empty and not able to detect any shape for the model to be designed for. Tuples for dimensionality play a major role in it.


Keras input plays a very pivotal role in designing and modeling as it involves enhanced data structure and Machine learning algorithm working in the background. Keras input consists of shapes and other attributes with respect to layers that further can provide errors in the values or arguments defined with the function.

Recommended Articles

This is a guide to Keras input. Here we discuss the definition, explanation, shape, Keras input code using Sequential API, attributes with layers. You may also have a look at the following articles to learn more –

How To Create A Dropdown List Using Javascript?

We will learn to create a dropdown list using HTML and JavaScript below. Before starting with the article, let’s understand the dropdown list and why we need to use it.

The dropdown list gives multiple choices to users and allows them to select one value from all options. However, we can do the same thing using multiple radio buttons, but what if we have hundreds of choices? Then we can use the dropdown menu.


function selectOption() {     let selectedValue = dropdown.options[dropdown.selectedIndex].text; } Example

In the example below, we have created the dropdown menu for car brands. Also, we have written the JavaScript code to get the selected value from the dropdown. The ‘onchange’ event will trigger whenever the user selects new values and invoke the selectOption() function.

Also, we have given some CSS styles to the default dropdown menu. Furthermore, we hide the dropdown menu’s arrow to improve it. In CSS, users can see how they can customize the default dropdown.

let output = document.getElementById(‘output’); function selectOption() { let dropdown = document.getElementById(‘dropdown’); let selectedIndex = dropdown.selectedIndex; let selectedValue = dropdown.options[selectedIndex].text; output.innerHTML = “The selected value is ” + selectedValue; }

We can use normal HTML, CSS, and JavaScript to create a dropdown menu from scratch. We can use HTML to make dropdowns, CSS to style them properly, and JavaScript to add behavior.


Users can follow the steps below to create a dropdown menu using HTML, CSS, and JavaScript.

Step 1 − Create a div element for the dropdown title, and style it using CSS. We have created the div element with the ‘menu-dropdwon’ class.

Step 2 − Create a div element with the ‘dropdown-list’ class to add dropdown options.

Step 4 − Now, use JavaScript to add the behavior to our dropdown.

Step 6 − In the openDropdown() function, access the div element with the class name ‘dropdown-list’ and show if it’s hidden or hides it if it is visible using the display property.


.menu-dropdown { width: 10rem; height: 1.8rem; font-size: 1.5rem; background-color: aqua; color: black; border: 2px solid yellow; border-radius: 10px; padding: 2px 5px; text-align: center; justify-content: center; cursor: pointer; } .dropdown-list { display: none; z-index: 10; background-color: green; color: pink; font-size: 1.2rem; width: 10.5rem; border-radius: 10px; margin-top: 0rem; cursor: pointer; } .dropdown-list p { padding: 3px 10px; } .dropdown-list p:hover { background-color: blue; } Choose Value let output = document.getElementById(‘output’); let dropdownList = document.getElementById(“list”); = “none”; function openDropdown() { if ( != “none”) { = “none”; } else { = “block”; } } const p_elements = document.getElementsByTagName(“p”); const totalP = p_elements.length; for (let i = 0; i < totalP; i++) { const option = p_elements[i]; output.innerHTML = “The selected option is ” + option.innerHTML; = “none”; }) }

How To Configure Mouse Wheel Speed Across Browsers Using Javascript?

We can use JavaScript to change the behavior of the web page. Every browser has a default scrolling speed when users scroll using the mouse wheel. However, we can control it using JavaScript.

We can also use the mouse wheel to zoom in and out of web pages. In such cases, we require to reduce the mouse wheel speed. Furthermore, developers sometimes require a scrolling speed limit, such as on website rules, so that users can read it properly.

In this tutorial, we will learn different ways to control the mouse’s wheel speed.


Users can follow the syntax below to use the ‘wheel’ event to configure mouse wheel speed across browsers using JavaScript.

let deltaY = event.deltaY; content.scrollTop += deltaY / n; });

We take the current scrolling speed using the deltaY property in the above syntax. After that, we divide the current scrolling speed by n to change the scrolling speed. Developers should increase the value of n to decrease the scrolling speed and decrease the value of n to increase the scrolling speed.

Example 1 (Using the Wheel Event)

In the example below, we have created the content div element and added the text content. In CSS, we have set the dimensions of the div element and set the overflow scroll to make the div scrollable.

In JavaScript, we used the addEventListner() method to fire the ‘wheel’ event and get the current scrolling speed. After that, we have decreased the scrolling speed by 50 times. In the output, users can try to scroll through the div element and observe the slow scrolling speed.

#content { height: 300px; width: 300px; overflow-y: scroll; padding: 10px; font-size: 20px; font-family: Arial, sans-serif; background-color: pink; } const content = document.getElementById(“content”); event.preventDefault(); let deltaY = event.deltaY;

let speed = deltaY / 50;

content.scrollTop += speed; }); Example 2 (Using the Mousewheel Event For Chrome Browser)

In the example below, we have used the ‘mousewheel’ event. When users use the mouse wheel to scroll the web page in the Chrome browser, it fires the ‘mousewheel’ event.

Here, we have multiplied the current scrolling speed by 0.03 to decrease the scrolling speed by 97%. However, it is very similar to the ‘wheel’ event.

#content { height: 200px; width: 200px; overflow-y: scroll; padding: 10px; font-size: 60px; font-family: Arial, sans-serif; background-color: aqua; } const content = document.getElementById(“content”); event.preventDefault(); const deltaY = event.deltaY; content.scrollTop += deltaY * 0.03; }); Example 3 (Using the DomMouseScroll Event for Firefox Browser)

In the example below, we have used the ‘DomMouseScroll’ event. The Firefox browser only supports it but not other browsers such as Chrome, opera, etc.

We used the ‘detail’ property of the event to get the current scrolling speed and multiplied it by 0.5 to reduce the scrolling speed by half. Users can open the below web page in the Firefox browser the observe the change in the scrolling speed of the div element.

#content { height: 200px; width: 200px; overflow-y: scroll; padding: 10px; font-size: 40px; background-color: green; } const content = document.getElementById(“content”); event.preventDefault(); const deltaY = event.detail; content.scrollTop += deltaY * 0.5; }); Example 4 (Customizing the Mouse Wheel Speed From the Web Page)

In the example below, we allow users to change the mouse wheel scrolling speed from the web browser. We have created the range slider that takes the input between 1 and 50. Users can change the value of the range slider.

After that, whenever users scroll the div element, JavaScript sets the scrolling speed according to the selected value in the range slider.

#content { height: 200px; width: 200px; overflow-y: scroll; padding: 10px; font-size: 20px; font-family: Arial, sans-serif; background-color: aqua; } const content = document.getElementById(“content”); event.preventDefault(); let value = document.getElementById(“myRange”).value; let deltaY = event.deltaY; deltaY = value; } else if (deltaY < 0) { deltaY = -value; } content.scrollTop += deltaY; });

Users learned to control the mouse wheel scrolling speed across browsers using JavaScript. This tutorial used three events to configure the mouse wheel speed. The first event is the ‘wheel’ event which is compatible with all browsers. The second event is ‘mousewheel’, compatible with all browsers except Firefox. The third event is ‘DOMMouseScroll’, which is only compatible with Firefox.

Update the detailed information about Practical Code Examples Using Javascript on the website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!