How to check if a string contains a substring in JavaScript

String.prototype.contains() ES6 browsers support a String.prototype.contains() method. But its not supported in older browsers still in use, such as Internet Explorer 11 and below, or Safari 7 and below. var string1 = "topjavatutorial"; var string2 = "java"; if (string1.contains(string2)) { console.log('string1 contains string2'); }   If you are still one of those browsers, we can […]

Running JavaScript from Java using Java 8 Nashorn engine

Nashorn engine Java 6 introduced a JavaScript engine called Rhino that enabled embedding JavaScript code in Java. In Java 8, a new JavaScript engine called Nashorn has been added that provides several new features.   To obtain Nashorn engine, we need to create an instance of ScriptEngineManager and then pass the engine name in getEngineByName() […]

Arrow functions in JavaScript

Arrow functions Arrow functions are, as the name suggests, functions defined with a new syntax that uses an arrow (=>) as part of the syntax. The arrow function definition consists of a parameter list followed by the => marker, which is followed by a function body. For example, (param1, param2, …, paramN) => { statements […]

JavaScript Scope

Scope   In JavaScript, scope refers to the current context of code. A variable’s scope is the context in which the variable exists. The scope specifies from where you can access a variable and whether you have access to the variable in that context. In JavaScript, a variable can be globally or locally defined   […]

JavaScript Switch statement

Switch statement   Switch statement starts with a variable called Switch value. This is the variable whose value if compared in each case statement within the switch. Each case statement indicates a possible value for the variable and subsequent code that should run if the variable matches the case value.   Example of Switch statement […]