How to use a ternary operator in JavaScript without else or empty? Is it possible? A ternary operation is called ternary because it takes 3 arguments, if it takes 2 it is a binary operation. It’s an expression returning a value. If you omit the else you would have an undefined situation where the expression Read More…

# Month: February 2021

## Ternary operator multiple conditions JavaScript | Example code

Ternary operator more than one condition is possible with nesting it. Let’s see example for that in this tutorial. Example of Ternary operator multiple conditions JavaScript HTML example code for If mark -> 1 then result = A If mark -> 2 then result = B else result = C

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<!DOCTYPE html> <html> <body> <script type="text/javascript"> var marks = 8; var result = (marks == 1) ? "A" : (marks == 2) ? "B" : "C"; console.log(result); </script> </body> </html> |

Output: JS Ternary functions Read More…

## JavaScript conditional operator | Example code

JavaScript conditional operator is assigned a value to a variable based on some condition. It’s the only JavaScript operator that takes three operands. It also called a ternary operator. Syntax of conditional operator in JS.

1 |
condition ? exprIfTrue : exprIfFalse |

JavaScript conditional operator Example Let’s see simple HTML example code for a given condition – If the value is Read More…

## JavaScript addition, subtraction, multiplication, division | Simple Examples

Addition, subtraction, multiplication, and division are basic Arithmetic operations in JavaScript. Addition Code: The addition operator (+) adds numbers:

1 2 3 |
var x = 5; var y = 5; var z = x + y; |

Subtraction code: The subtraction operator (-) subtracts numbers.

1 2 3 |
var x = 10; var y = 5; var z = x - y; |

Multiplication Code: The multiplication operator (*) multiplies numbers.

1 2 3 |
var x = 5; var y = 2; var z = x * y; |

Division Code: The division operator (/) divides numbers.

1 2 3 |
var x = 10; var y = 2; var z = x / y; |

JavaScript addition, subtraction, multiplication, division Examples HTML complete Read More…

## Write a JavaScript program that calculates the product of two numbers and returns the result

In this tutorial you will learn with an example where the user clicks the button after entering the number in their respective text boxes, the result will get displayed through the alert box. It’s a very easy JavaScript Example code of calculates the product of two numbers and returns the result. JavaScript example of calculating Read More…

## Initializing an Array with a Single Value in JavaScript | Example code

How to initialize all members of an array to the same value in JS? There are several ways to create an array in JavaScript to Initializing an Array with a Single Value. Like Array Constructor, map, Array.from(), etc. Let’s See HTML examples for some of methods Array Constructor Use Array Constructor to create an array Read More…

## JavaScript recursive function array | Compute the sum of an array of integers

A JavaScript recursive function is a function that calls itself until it doesn’t. It has a condition to stop calling itself, otherwise, it will call itself indefinitely. JavaScript recursive function array Example code HTML example code program to compute the sum of an array of integers in JavaScript.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<!DOCTYPE html> <html> <body> <script type="text/javascript"> var array_sum = function(my_array) { if (my_array.length === 1) { return my_array[0]; } else { return my_array.pop() + array_sum(my_array); } }; console.log(array_sum([1,2,3,4,5,6])); </script> </body> </html> |

Output: Do comment if you have Read More…

## Sum of numbers from 1 to n JavaScript recursion | Example code

You don’t need to loop through each pair and add them in recursion. Just use if statement as below example. Sum of numbers from 1 to n Examples in JS Let’s see HTML example code:- Sum of numbers from 1 to n JavaScript recursion Check the value and if truthy return n plus the sum Read More…

## Create a function to count numbers between two given numbers in JavaScript

Count the number of values between 2 user input values with an array Create function with login and execute only on button click. Get the the inputs with getElementById and store the values given by the user inside num1 and num2 variables. HTML Example code:-

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<!DOCTYPE html> <html> <body> <input type="text" id="num1"> <input type="text" id="num2"> <button onclick="GetNumCount()" type="button">search</button> <script type="text/javascript"> function GetNumCount() { var total = 0; var num1 = document.getElementById('num1').value; var num2 = document.getElementById('num2').value; var array = [1, 4, 6, 7, 8, 6]; for (var a = 0; a < array.length; a++) { if (array[a] >= num1 && array[a] <= num2) { total++; } } alert("Total numbers of values = " + total); } </script> </body> </html> |

Output: Create a function to count numbers between two Read More…

## JavaScript generates a unique id | 3 Ways with examples code

How to Generate Unique ID in JavaScript? Using UUID module or Math.random method or Date.now method can generate a unique identifier (id) in JavaScript. JavaScript generates a unique id Examples Let’s see all ways example in HTML and JS:- Using UUID library To create a random UUID… 1. Install

1 |
npm install uuid |

2. Create a UUID (ES6 Read More…