var arr = [10, 20, 30, 40, 50];var max = arr[0];for(var i=0; i < arr.length; i++) { var a = arr[i]; if(a > max){ max = a; }}console.log(“Largest in the array: “, max);
2. Sum of all the elements in an array
var arr = [10, 20, 30, 40, 50];var sum = 0;for(var i=0; i < arr.length; i++) { var a = arr[i]; sum += a;}console.log(“Sum of the elements in the array: “, sum);
3. Remove duplicate items from an array
var arr = [10, 20, 30, 20, 40, 30…
The common misconception is that React is framework but it is not. React is a popular JS library to build UI.
Let’s deep into the dive with React.
To create react app
npx create-react-app react-app
command is required. And it will start the process of creating single page web application.
2. Directory view and Run the app
After creation of react app it will create a directory with the app name in the current directory.
react-app
├── README.md
├── node_modules
├── package.json
├── .gitignore
├── public
│ ├── favicon.ico
│ ├── index.html
│ └── manifest.json
└── src
├── App.css
├── App.js
├── App.test.js
├── index.css
├── index.js
├── logo.svg
└── serviceWorker.js …
While writing codes it is very common that error happens and to handle these errors JS have 4 statements. These are try, catch, throw and finally.
Syntax:
try {
Lines of code
}
catch(err) {
Lines of code to check errors
}
finally {
Lines of code which must be executed after try and catch
}
Example:
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8">
<meta name=”viewport” content=”width=device-width, initial-scale=1.0">
<title>Error Handling</title>
</head>
<body>
<h3>Error Handling in JS</h3>
<p id=”errorMessage”></p>
<p id=”finally”></p>
<script type = “text/javascript”>
try
{
const result = Multiplication(10, 20); // Multiplication is not defined
}
catch(err){
document.getElementById(“error Message”).innerHTML = err;
}
finally{
document.getElementById(“finally”).innerHTML = “JS error check done!”; …
Syntax:
string.charAt(index)
Example:
var sampleString = “New String”;
var requiredCharacter = sampleString.charAt(0);
console.log(requiredCharacter);
If you to return the last character of the string then
var sampleString = “New String”;
var lastCharacter = sampleString.charAt(sampleString.length-1);
console.log(requiredCharacter);
2. concat()
Sometimes you do not need to change the existing strings, but return a new string combining two or more strings then you may use the concat() method. …