Found 9150 Articles for Object Oriented Programming

How to change the color of a button when the input field is filled – JavaScript?

AmitDiwan
Updated on 09-Nov-2020 08:34:56

2K+ Views

Let’s say the following is our button −Press MeOn filling the below input field, the color of the above button should change −ExampleFollowing is the code − Live Demo            Document           UserName:                Press Me    function changeTheColorOfButtonDemo() {       if (document.getElementById("changeColorDemo").value !== "") {          document.getElementById("buttonDemo").style.background = "green";       } else {          document.getElementById("buttonDemo").style.background = "skyblue";       }    } To run ... Read More

In JavaScript, what is meant by 'a function expression is always a constant value'?

AmitDiwan
Updated on 09-Nov-2020 08:27:33

131 Views

If the const is used in a program, and if you try to reassign the value to const variable then an error will arise.Let’s say the following is our const variable −const result = (first, second) => first * second;Now, we will try to reassign a value to the const variable and an erro can be seen in the output.ExampleFollowing is the code −const result = (first, second) => first * second; result = first => first =first*10; console.log(result(10, 20)); To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo284.js.OutputThis will produce ... Read More

Hide a video tag on a web page - JavaScript

AmitDiwan
Updated on 09-Nov-2020 08:18:01

4K+ Views

Let’s say we have the following sample video tag on a web page        You cannot play video here...... To hide a video on a web page, use yourVariableName.style.display=’none’.ExampleFollowing is the code −            Document    .hideVideo {       display: block;       z-index: 999;       margin-top: 10px;       margin-left: 10px;    }               You cannot play video here......        var hideVideo = document.getElementsByClassName("hideVideo")[0];    hideVideo.style.display = ... Read More

Implement Bubble sort with negative and positive numbers – JavaScript?

AmitDiwan
Updated on 09-Nov-2020 08:13:49

904 Views

Let’s say the following is our unsorted array with negative and positive numbers −var arr = [10, -22, 54, 3, 4, 45, 6];ExampleFollowing is the code to implement Bubble Sort −function bubbleSort(numberArray, size) {    for (var lastIndex = size - 1; lastIndex > 0; lastIndex--) {       for (var i = 0; i < lastIndex; i++) {          if (numberArray[i] > numberArray[i + 1]) {             var temp = numberArray[i];             numberArray[i] = numberArray[i + 1];             numberArray[i + ... Read More

What happens when length of object is set to 0 - JavaScript?

AmitDiwan
Updated on 09-Nov-2020 08:07:12

350 Views

Let’s say the following is our array object −var arrayObject =    [       "John",       "David",       "Mike"    ]You can use length property to set the length to 0 and clear memoryThe syntax is as follows to clear memory −yourArrayObjectName.length=0; // To clear memory yourArrayObjectName.length=4; // To allocate memoryOutputThis will produce the following output on console −var arrayObject =    [       "John",       "David",       "Mike"    ] arrayObject.length = 0; console.log(arrayObject); arrayObject.length = 5; for (var i = 0; i < arrayObject.length; i++)   ... Read More

How to make a condition for event 'click' inside/outside for multiple divs - JavaScript?

AmitDiwan
Updated on 09-Nov-2020 08:02:07

1K+ Views

You can use event listeners for clicks.ExampleFollowing is the code − Live Demo            Document           First Division               Second Division      document.addEventListener('click', callEventFuncion)    function callEventFuncion(event) {       var div = document.querySelectorAll('.divDemo');       var titleResult = document.querySelectorAll('.my-title');       var result = Array.apply(0, div).find((v) => v.contains(event.target));       if (result) {          console.log(" Incrementing Division Selection");       }       else { ... Read More

How to concatenate the string value length -1 in JavaScript.

AmitDiwan
Updated on 09-Nov-2020 07:54:56

300 Views

For this, use join(). It will concatenate the string value length-1.ExampleFollowing is the code −var count = 5; var values = new Array(count + 1).join('John'); console.log(values); var count1 = 5; var values1 = new Array(count1).join('John'); console.log(values1);To run the above program, you need to use the following command −node fileName.js. Here, my file name is demo274.js.OutputThis will produce the following output on console −PS C:\Users\Amit\javascript-code> node demo274.js JohnJohnJohnJohnJohn JohnJohnJohnJohn

Using JSON.stringify() to display spread operator result?

AmitDiwan
Updated on 09-Nov-2020 07:46:10

1K+ Views

With Spread Operator, allow the expression to expand to multiple arguments, elements, variables, etc.You can use JSON.stringify() to convert the JavaScript object to string. Here, we have our object as the result of using spread operator on details1 and details2.ExampleFollowing is the code −var details1 = { name: 'John', age: 21 }; var details2 = { countryName: 'US', subjectName:'JavaScript' }; var result= { ...details1, ...details2}; console.log(JSON.stringify(result));To run the above program, you need to use the following command −node fileName.js. Here, my file name is demo267.js.OutputThis will produce the following output on console −PS C:\Users\Amit\javascript-code> node demo267.js {"name":"John", "age":21, "countryName":"US", "subjectName":"JavaScript"}Read More

Validate Tutorialspoint URL via JavaScript regex?

AmitDiwan
Updated on 09-Nov-2020 07:31:44

203 Views

To validate a specific URL, use regular expression.ExampleThe code is as follows −function validateSoundURL(myURL) {    var regularExpression = /^https?:\/\/(tutorialspoint\.com)\/(.*)$/;    return myURL.match(regularExpression) && myURL.match(regularExpression)[2] } console.log(validateSoundURL("https://tutorialspoint.com/index")); console.log(validateSoundURL("https://tutorialspoint.com/java"));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo259.js.OutputThis will produce the following output on console −PS C:\Users\Amit\javascript-code> node demo259.js index java

Is the !! (not not) operator in JavaScript equivalent to reverse process of not operator?

AmitDiwan
Updated on 09-Nov-2020 06:25:30

237 Views

Yes, the not not operator is the reverse process of not operator. If any value is true then single ! (not) will return false and !! will return opposite value (true).The not operator −var flag=true; console.log(!flag);The not not operator −var flag=true; console.log(!!flag);ExampleFollowing is the code −var flag=true; console.log("The result of single !=") console.log(!flag); console.log("The result of single !!=") console.log(!!flag)To run the above program, you need to use the following command −node fileName.js. Here, my file name is demo247.jsOutputThis will produce the following output on console −PS C:\Users\Amit\javascript-code> node demo247.js The result of single != false The result of single !!= ... Read More

Advertisements