Javascript – AppDividend https://appdividend.com Latest Code Tutorials Sat, 23 Mar 2019 16:27:03 +0000 en-US hourly 1 https://wordpress.org/?v=5.1.1 https://appdividend.com/wp-content/uploads/2017/08/cropped-ApDivi-32x32.png Javascript – AppDividend https://appdividend.com 32 32 Javascript Object Freeze Example | Object.freeze() Tutorial https://appdividend.com/2019/03/15/javascript-object-freeze-example-object-freeze-tutorial/ https://appdividend.com/2019/03/15/javascript-object-freeze-example-object-freeze-tutorial/#respond Fri, 15 Mar 2019 17:45:24 +0000 http://localhost/wordpress/?p=5225 Javascript Object Freeze Example | Object.freeze() Tutorial

Javascript Object Freeze Example | Object.freeze() Tutorial is today’s topic. The Object.freeze() method freezes an object. The frozen object can no longer be changed; freezing an object prevents new properties from being added to it, existing properties from being removed, prevents changing the enumerability, configurability, or writability of existing properties, and prevents the values of existing properties […]

The post Javascript Object Freeze Example | Object.freeze() Tutorial appeared first on AppDividend.

]]>
Javascript Object Freeze Example | Object.freeze() Tutorial

Javascript Object Freeze Example | Object.freeze() Tutorial is today’s topic. The Object.freeze() method freezes an object. The frozen object can no longer be changed; freezing an object prevents new properties from being added to it, existing properties from being removed, prevents changing the enumerability, configurability, or writability of existing properties, and prevents the values of existing properties from being changed.

Javascript Object Freeze Example

The syntax of Javascript Object.freeze() Tutorial is following.

Object.freeze(obj)

The obj argument is the object which needs to freeze.

Let’s see the following example.

// app.js

const student = { name: 'KRUNAL', education: 'BE IT' };
const freezedObj = Object.freeze(student);
freezedObj.name = 'ANKIT';
console.log(freezedObj);

The output is following.

 

Javascript Object Freeze Example

See, after freezing the student object, the property is not changed. We changed it manually, but still, it remains the same object as we have defined.

Freezing an object also prevents its prototype from being changed. The freeze() method returns the same object that was passed in.

Use strict mode

When we can write the above code in the strict mode, it will give an exception. See the following example.

// app.js

'use strict';

const student = { name: 'KRUNAL', education: 'BE IT' };
const freezedObj = Object.freeze(student);
freezedObj.name = 'ANKIT';
console.log(freezedObj);

The output is following.

 

Object.freeze() Tutorial

Nothing can be added to or removed from the properties set of the frozen object. Any attempt to do so will fail, either silently or by throwing the TypeError exception (most commonly, but not exclusively, when in strict mode).

The freeze() method returns the same object that was passed into the function. It does not create the frozen copy. For data properties of the frozen object, values cannot be changed; the writable and configurable attributes are set to false.

Finally, Javascript Object Freeze Example | Object.freeze() Tutorial is over.

The post Javascript Object Freeze Example | Object.freeze() Tutorial appeared first on AppDividend.

]]>
https://appdividend.com/2019/03/15/javascript-object-freeze-example-object-freeze-tutorial/feed/ 0
Javascript Object Values Example | Object.values() Tutorial https://appdividend.com/2019/03/15/javascript-object-values-example-object-values-tutorial/ https://appdividend.com/2019/03/15/javascript-object-values-example-object-values-tutorial/#respond Fri, 15 Mar 2019 16:39:22 +0000 http://localhost/wordpress/?p=5213 Javascript Object Values Example | Object.values() Tutorial

Javascript Object Values Example | Object.values() Tutorial is today’s topic. The Object.values() method returns the array of the given object’s enumerable property values. The ordering of the properties is the same as that given by the object manually is the loop is applied to the properties.  Javascript Object.values() takes an object as an argument of which […]

The post Javascript Object Values Example | Object.values() Tutorial appeared first on AppDividend.

]]>
Javascript Object Values Example | Object.values() Tutorial

Javascript Object Values Example | Object.values() Tutorial is today’s topic. The Object.values() method returns the array of the given object’s enumerable property values. The ordering of the properties is the same as that given by the object manually is the loop is applied to the properties. 

Javascript Object.values() takes an object as an argument of which an enumerable own property values are to be returned and returns the array containing all the enumerable property values of the given object.

Javascript Object Values Example

The syntax of Javascript Object.values() is following.

Object.values(obj)
An obj parameter whose enumerable own property values are to be returned.
The values() function returns an array containing the given object’s enumerable property values.

Let’s see the following example.

// app.js

let student = { name: 'KRUNAL', education: 'BE IT' };
console.log(Object.values(student));

The output is following.

 

Javascript Object Values Example

So, it has returned an array of values of the passed object.

We can do the same thing using the for…in loop. See the following example.

// app.js

let student = { name: 'KRUNAL', education: 'BE IT' };
let arr = [];
for (const prop in student) {
  arr.push(student[prop]);
}
console.log(arr);

The output is following.

 

Javascript For In Loop Tutorial

But, we do not have to use for…in loop whenever we have a ready-made method available to get the values of an object in the form of an array.

You can get an array of keys with Object.keys() and then use the map() method to get values. See the following example.

// app.js

let student = { name: 'KRUNAL', education: 'BE IT' };
let output = Object.keys(student).map(function(e) {
  return student[e]
})
console.log(output);

It will give us the same output.

Finally, Javascript Object Values Example | Object.values() Tutorial is over.

The post Javascript Object Values Example | Object.values() Tutorial appeared first on AppDividend.

]]>
https://appdividend.com/2019/03/15/javascript-object-values-example-object-values-tutorial/feed/ 0
Javascript Array Includes Example | Array.prototype.includes() Tutorial https://appdividend.com/2019/03/14/javascript-array-includes-example-array-prototype-includes-tutorial/ https://appdividend.com/2019/03/14/javascript-array-includes-example-array-prototype-includes-tutorial/#respond Thu, 14 Mar 2019 12:15:36 +0000 http://localhost/wordpress/?p=5198 Javascript Array Includes Tutorial

Javascript Array Includes Example | Array.prototype.includes() Tutorial is today’s topic. The includes() method determines whether the array contains the specified element. It returns true or false as output depending on the result. The Javascript Array includes() method is case sensitive. Javascript Array Includes Example The syntax of Javascript Array Includes is following. array.includes(element, start) The element parameter is […]

The post Javascript Array Includes Example | Array.prototype.includes() Tutorial appeared first on AppDividend.

]]>
Javascript Array Includes Tutorial

Javascript Array Includes Example | Array.prototype.includes() Tutorial is today’s topic. The includes() method determines whether the array contains the specified element. It returns true or false as output depending on the result. The Javascript Array includes() method is case sensitive.

Javascript Array Includes Example

The syntax of Javascript Array Includes is following.

array.includes(element, start)

The element parameter is required, and it is the element which we need to search.

The start parameter is an optional parameter, and the default is 0. It is the position in the array to start the search.

Let’s take an example of Javascript Array Includes.

// app.js

let arr = ['Gavin', 'Richard', 'Erlich', 'Gilfoyle'];

console.log(arr.includes('Erlich'));

As we know, Erlich is in the arr. So it will return true.

 

Javascript Array Includes Example

If we search for an element which does not exist in an array, then it will return the false.

// app.js

let arr = ['Gavin', 'Richard', 'Erlich', 'Gilfoyle'];

console.log(arr.includes('Jared'));

The output is following.

 

Array.prototype.includes() Tutorial

When comparing strings and characters, includes() function is case-sensitive.

The includes() uses the sameValueZero algorithm to determine whether the given element is found.

Let’s see other examples.

// app.js

console.log([1, 2, 3].includes(3, 2));
console.log([19, 21, 46].includes(19, 1));
console.log([19, 21, 46].includes(46, -1));
console.log([1, 2, NaN].includes(NaN));

The output of the above code is following.

 

Javascript Array Includes Example | Array.prototype.includes() Tutorial

If the start position is greater than or equal to the length of the array, then false is returned. The array will not be searched.

If the start position is negative, the computed index is calculated to be used as a position in an array at which to begin searching for the element. If the computed index is less or equal than -1 * array.length, an entire array will be searched.

Finally, Javascript Array Includes Example | Array.prototype.includes() Tutorial is over.

The post Javascript Array Includes Example | Array.prototype.includes() Tutorial appeared first on AppDividend.

]]>
https://appdividend.com/2019/03/14/javascript-array-includes-example-array-prototype-includes-tutorial/feed/ 0
ES6 Modules in Node Tutorial With Example https://appdividend.com/2019/03/13/es6-modules-in-node-tutorial-with-example/ https://appdividend.com/2019/03/13/es6-modules-in-node-tutorial-with-example/#respond Wed, 13 Mar 2019 17:44:57 +0000 http://localhost/wordpress/?p=5185 ES6 Modules in Node Tutorial With Example

ES6 Modules in Node Tutorial With Example is today’s topic. ES6 Modules are useful when we need to make objects, functions, classes or variables available to the outside world it’s as simple as exporting them and then importing them where needed in other files. With ES2015 (ES6) evolution, we get built-in support for modules in […]

The post ES6 Modules in Node Tutorial With Example appeared first on AppDividend.

]]>
ES6 Modules in Node Tutorial With Example

ES6 Modules in Node Tutorial With Example is today’s topic. ES6 Modules are useful when we need to make objects, functions, classes or variables available to the outside world it’s as simple as exporting them and then importing them where needed in other files. With ES2015 (ES6) evolution, we get built-in support for modules in JavaScript like with CommonJS, in which each file is its module. If you do not know about import in es6, then check out my Javascript Import Statement guide.

ES6 Modules in Node

JavaScript has had modules for a long time. However, they were implemented via libraries, not built into the language. ES6 is the first time that JavaScript has built-in modules. ES6 modules are stored in files.

Exporting Module in Javascript

You can export members one by one. What’s not exported won’t be available directly outside the module. 

We are practicing this example in Node.js. So first, we need to create a file called app.mjs with the .mjs extension.

Node.js does not support ES6 Module functionality directly. So we need to use its experimental feature.

Write the following code inside the app.mjs file.

// app.mjs

export const sqrt = Math.sqrt;
export function square(x) {
    return x * x;
}

We are exporting two functions called squrt and square from the app.mjs module.

We will use the –experimental-modules flag can be used to enable features for loading ESM modules.

Now, create one more file called server.mjs and add the following code.

// server.mjs

import { sqrt, square } from './app';

const a = sqrt(4);
const b = square(2);
console.log(a);
console.log(b);

Now, to run the above file, we need to add a flag called –experimental-modules.

So type the following command.

node --experimental-modules server.mjs

The output is following.

 

ES6 Modules in Browser and Node

There is also another way to run ES6 modules in node.js. But for that, you need to install babel dependencies.

First, go to your terminal and go to inside your project root and type the following command.

npm init -y

Then we need to install two node modules dependencies. So type the following command.

npm install babel-register babel-preset-env --save-dev

It will install the modules inside the node_modules folder.

Now, create a file called start.js write the following code inside the start.js file.

// start.js

require('babel-register')({
    presets: ['env']
});

module.exports = require('./app.js')

Now, create another file inside the root called process.js and add the following code.

// process.js

export const sqrt = Math.sqrt;
export function square(x) {
    return x * x;
}

Finally, add the following code inside the app.js file.

// app.js

import { sqrt, square } from './process';
const a = sqrt(4);
const b = square(2);
console.log(a);
console.log(b);

Now, we will run the start.js file, and we will see the output in the console.

 

ES6 Modules in Browser and Node Tutorial With Example

See, we get the same thing, but this time, we have used the babel libraries to transpile the ES6 code and make use of ES6 modules inside Node.js.

That means, we have seen the two ways to use ES6 modules on the server side or node.js side.

Import Complete Module

We can import the complete module using the following code inside the app.js file.

// app.js

import * as func from './process';
const a = func.sqrt(4);
const b = func.square(2);
console.log(a);
console.log(b);

It will give us the same output. We have a complete import module as a func.

Single Default Module Export

If your export file has only one default module, then you can do it following way.

Write the following code inside the process.js file.

// process.js

export default function square(x) {
    return x * x;
}

Now, you can import this file inside the app.js file using the following way without curly braces.

// app.js

import square from './process';
const b = square(2);
console.log(b);

The output will be in the console. But you can see in the code that when we used the import statement, we have not used the curly braces.

Modules in JavaScript

In Javascript, each module is a piece of code that is executed once it is loaded.

A module can import things from other modules. It refers to those modules via module specifiers.

Modules are singletons. Even if a module is imported multiple times, only a single “instance” of it exists.

Finally, ES6 Modules in Browser and Node Tutorial With Example is over.

The post ES6 Modules in Node Tutorial With Example appeared first on AppDividend.

]]>
https://appdividend.com/2019/03/13/es6-modules-in-node-tutorial-with-example/feed/ 0
Javascript Array FlatMap Example | Array.prototype.flatMap() Tutorial https://appdividend.com/2019/03/13/javascript-array-flatmap-example-array-prototype-flatmap-tutorial/ https://appdividend.com/2019/03/13/javascript-array-flatmap-example-array-prototype-flatmap-tutorial/#respond Wed, 13 Mar 2019 08:28:49 +0000 http://localhost/wordpress/?p=5169 Javascript Array FlatMap Example Tutorial

Javascript Array FlatMap Example | Array.prototype.flatMap() Tutorial is today’s topic. The flatMap() method first maps each element using the map function, then flattens the result into the new array.  It is identical to the Javascript Array map followed by the Javascript Array flat of depth 1, but flatMap() method is often quite useful, as merging both into one method is slightly more […]

The post Javascript Array FlatMap Example | Array.prototype.flatMap() Tutorial appeared first on AppDividend.

]]>
Javascript Array FlatMap Example Tutorial

Javascript Array FlatMap Example | Array.prototype.flatMap() Tutorial is today’s topic. The flatMap() method first maps each element using the map function, then flattens the result into the new array.  It is identical to the Javascript Array map followed by the Javascript Array flat of depth 1, but flatMap() method is often quite useful, as merging both into one method is slightly more efficient.

Javascript Array FlatMap Example

The array.flatMap() is an inbuilt Javascript function which is used to flatten the input array element into the new array. Array flatMap() first of all map every element with the help of mapping function, then flattens the input array element into the new array.

The syntax for flatMap() function is following.

let new_array = arr.flatMap(function callback(currentValue[, index[, array]]) {
    // return element for new_array
}[, thisArg])

It returns a new array with each element being the result of the callback function and flattened to a depth of 1. It has a callback function as a parameter which has the following arguments.

The currentValue is the current element being processed in the array.

The index is an optional parameter, and it is the index of the current element being processed in the array.

An array is an optional argument, and it is the array on which map function is called.

Let’s see the following example.

// app.js

let arrA = [1, 2, 3, 4];
let arrB = arrA.flatMap(x => [x * 2]);
console.log(arrB);

The output is following.

 

Javascript Array FlatMap Example | Array.prototype.flatMap() Tutorial

Let’s see another example.

// app.js

let arrA = [1, 2, 3, 4];
let arrB = arrA.flatMap(x => [[x * 2]]);
console.log(arrB);

The output is following.

 

Javascript Array FlatMap Example

Finally, Javascript Array FlatMap Example Tutorial is over.

The post Javascript Array FlatMap Example | Array.prototype.flatMap() Tutorial appeared first on AppDividend.

]]>
https://appdividend.com/2019/03/13/javascript-array-flatmap-example-array-prototype-flatmap-tutorial/feed/ 0
Javascript Array Flat Example | Array.prototype.flat() Tutorial https://appdividend.com/2019/03/12/javascript-array-flat-example-array-prototype-flat-tutorial/ https://appdividend.com/2019/03/12/javascript-array-flat-example-array-prototype-flat-tutorial/#respond Tue, 12 Mar 2019 12:43:02 +0000 http://localhost/wordpress/?p=5151 Flatten a nested array in Javascript

Javascript Array Flat Example | Array.prototype.flat() Tutorial is today’s topic. Javascript Array flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. It returns a new array with the sub-array elements concatenated into it. Javascript Array Flat Example As its name suggests, the Array flat() method available on […]

The post Javascript Array Flat Example | Array.prototype.flat() Tutorial appeared first on AppDividend.

]]>
Flatten a nested array in Javascript

Javascript Array Flat Example | Array.prototype.flat() Tutorial is today’s topic. Javascript Array flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. It returns a new array with the sub-array elements concatenated into it.

Javascript Array Flat Example

As its name suggests, the Array flat() method available on an Array prototype returns the new array that’s the flattened version of an array it was called on. If we do not provide arguments passed-in, then the depth of 1 is assumed.

The syntax of Javascript Array flat() method is following.

let newArray = arr.flat([depth]);

The depth level is specifying how deep a nested array structure should be flattened. Defaults to 1. It is an optional parameter.

Let’s see the following example.

// app.js

const arr = [['krunal', 'ankit'], [21, 74]];

console.log(arr.flat());

The output is following.

 

Javascript Array Flat Example | Array.prototype.flat() Tutorial

In the above example, by default, the depth level is 1. Let’s see the example where depth level is more than 1.

// app.js

const arr = [['krunal', 'ankit'], [21, 74], ['appdividend', ['Laravel', 'Angular']]];

console.log(arr.flat(2));

The output is following.

 

Javascript Array Flat Example

You can use Infinity if you need to flatten an array of the arbitrary depth.

See the following example.

// app.js

const arr = [['krunal', 'ankit'], [21, 74], ['appdividend', ['Laravel', 'Angular', ['easy']]]];

console.log(arr.flat(Infinity));

The output is following.

 

Array.prototype.flat() Tutorial

Flattening and array holes

The flat method removes empty slots in arrays. See the following example.

// app.js

const arr = ['WhatsApp', 'Facebook', , 'Twitter'];

console.log(arr.flat());

See the following output.

 

Flattening and array holes

Finally, Javascript Array Flat Example | Array.prototype.flat() Tutorial is over.

The post Javascript Array Flat Example | Array.prototype.flat() Tutorial appeared first on AppDividend.

]]>
https://appdividend.com/2019/03/12/javascript-array-flat-example-array-prototype-flat-tutorial/feed/ 0
Javascript String StartsWith Example | String.prototype.startsWith() Tutorial https://appdividend.com/2019/03/12/javascript-string-startswith-example-string-prototype-startswith-tutorial/ https://appdividend.com/2019/03/12/javascript-string-startswith-example-string-prototype-startswith-tutorial/#respond Tue, 12 Mar 2019 10:02:59 +0000 http://localhost/wordpress/?p=5140 String startsWith Example

Javascript String StartsWith Example | String.prototype.startsWith() Tutorial is today’s topic. The string startsWith() method determines whether a string begins with the characters of a specified string, returning true or false as appropriate. It is checking if a string starts with another substring is a common thing to do. See how to perform this check in JavaScript. Javascript […]

The post Javascript String StartsWith Example | String.prototype.startsWith() Tutorial appeared first on AppDividend.

]]>
String startsWith Example

Javascript String StartsWith Example | String.prototype.startsWith() Tutorial is today’s topic. The string startsWith() method determines whether a string begins with the characters of a specified string, returning true or false as appropriate. It is checking if a string starts with another substring is a common thing to do. See how to perform this check in JavaScript.

Javascript String StartsWith Example

ES6, introduced in 2015, added the startsWith() method to the String object prototype. This means you can call startsWith() function on any string, provide a substring, and check if the result returns true or false.

The syntax of String startsWith() is following.

str.startsWith(searchString[, position])

searchString: The characters to be searched for at the start of this string.

position: OptionalThe position in this string at which to begin searching for searchString; defaults to 0.

Let’s see the following example.

// app.js

const str1 = 'Captain Marvel movie is somewhat boring';

console.log(str1.startsWith('Cap'));

console.log(str1.startsWith('Marvel', 8));

The output is following.

 

Javascript String StartsWith Example

String character starts with 0th position. So above will return the true. Let’s see a scenario where we get false.

// app.js

const str1 = 'Captain Marvel movie is somewhat boring';

console.log(str1.startsWith('Marvel'));

console.log(str1.startsWith('Marvel', 9));

The output is following.

 

String.prototype.startsWith() Tutorial

Finally, Javascript String.prototype.startsWith() Tutorial is over.

The post Javascript String StartsWith Example | String.prototype.startsWith() Tutorial appeared first on AppDividend.

]]>
https://appdividend.com/2019/03/12/javascript-string-startswith-example-string-prototype-startswith-tutorial/feed/ 0
Javascript String trimStart() trimEnd() Tutorial With Example https://appdividend.com/2019/03/11/javascript-string-trimstart-trimend-tutorial-with-example/ https://appdividend.com/2019/03/11/javascript-string-trimstart-trimend-tutorial-with-example/#respond Mon, 11 Mar 2019 12:08:28 +0000 http://localhost/wordpress/?p=5098 Javascript String trimStart() trimEnd() Tutorial With Example From Scratch

Javascript String trimStart() trimEnd() Tutorial With Example is today’s topic. The trimStart() method removes whitespace from the beginning of the string. The trimLeft() is an alias of this method. The trimEnd() method removes whitespace from the end of a string. The trimRight() is an alias of this method. Javascript String trimStart() trimEnd() Tutorial Javascript String trimStart() or trimLeft() do […]

The post Javascript String trimStart() trimEnd() Tutorial With Example appeared first on AppDividend.

]]>
Javascript String trimStart() trimEnd() Tutorial With Example From Scratch

Javascript String trimStart() trimEnd() Tutorial With Example is today’s topic. The trimStart() method removes whitespace from the beginning of the string. The trimLeft() is an alias of this method. The trimEnd() method removes whitespace from the end of a string. The trimRight() is an alias of this method.

Javascript String trimStart() trimEnd() Tutorial

Javascript String trimStart() or trimLeft() do not affect the value of the string itself.

First, let’s see the example of Javascript String trimStart() method.

// app.js

let app = '   Human Nature!   ';
console.log(app);
console.log(app.trimStart());

The output of the above code is following.

 

Javascript String trimStart() Tutorial

It returns a new string with removed white space from the start of the original string.

Javascript String trimEnd() or trimRight() do not affect the value of the string itself.

Okay, now let’s see the example of a Javascript String trimEnd() method.

// app.js

let app = '   Human Nature!   ';
console.log(app);
console.log(app.trimEnd());

The output is following.

 

Javascript String trimStart() trimEnd() Tutorial

It returns a new string representing the calling string stripped of whitespace from its (right) end.

We can count the length of the strings to see if the whitespaces are trimmed or not.

// app.js

let app = '   Human Nature!   ';
console.log('Before trimmed', app.length);
console.log('After trimmed',app.trimEnd().length);

The output is following.

 

Javascript String trimStart() trimEnd() Tutorial With Example

Finally, Javascript String trimStart() trimEnd() Tutorial With Example is over.

The post Javascript String trimStart() trimEnd() Tutorial With Example appeared first on AppDividend.

]]>
https://appdividend.com/2019/03/11/javascript-string-trimstart-trimend-tutorial-with-example/feed/ 0
Javascript Error Object Tutorial With Example https://appdividend.com/2019/02/25/javascript-error-object-tutorial-with-example/ https://appdividend.com/2019/02/25/javascript-error-object-tutorial-with-example/#respond Mon, 25 Feb 2019 18:53:40 +0000 http://localhost/wordpress/?p=4957 Error in Javascript

Javascript Error Object Tutorial With Example is today’s topic. The Error constructor creates an error object. Instances of Error objects are thrown when runtime errors occur. The Error object can also be used as a base object for user-defined exceptions. Runtime errors result in new Error objects being created and thrown. The Error object provides error […]

The post Javascript Error Object Tutorial With Example appeared first on AppDividend.

]]>
Error in Javascript

Javascript Error Object Tutorial With Example is today’s topic. The Error constructor creates an error object. Instances of Error objects are thrown when runtime errors occur. The Error object can also be used as a base object for user-defined exceptions. Runtime errors result in new Error objects being created and thrown. The Error object provides error information when an error occurs.

Javascript Error Object Tutorial With Example

The syntax of Error in Javascript is following.

new Error([message[, fileName[, lineNumber]]])

message:

Optional. A human-readable description of the error.

fileName:

Optional. The value for the file property on the created Error object. Defaults to the name of the file containing the code that called the Error() constructor.

lineNumber:

Optional. The value for the lineNumber property on the created an Error object. Defaults to the line number containing the Error() constructor invocation.

Let’s see the Error in an example.

// app.js

const x = new Error('Oops!! There is a mistake');
console.log(x);

The output is following.

 

Javascript Error Tutorial With Example

The global Error object contains no methods of its own. However, it does inherit some methods from the prototype chain.

Javascript Error types

There are following types of Error in Javascript.

EvalError: Creates an instance representing an error that occurs regarding the global function eval().

InternalError:  Creates an instance representing an error that occurs when an internal error in the JavaScript engine is thrown. E.g. “too much recursion.”

RangeError: Creates an instance representing an error that occurs when a numeric variable or parameter is outside of its valid range.

ReferenceError: Creates an instance representing an error that occurs when de-referencing an invalid reference.

SyntaxError: Creates an instance representing a syntax error that occurs while parsing code in eval().

TypeError: Creates an instance representing an error that occurs when a variable or parameter is not of a valid type.

URIError: Creates an instance representing an error that occurs when encodeURI() or decodeURI() are passed invalid parameters.

Finally, Javascript Error Object Tutorial With Example is over.

The post Javascript Error Object Tutorial With Example appeared first on AppDividend.

]]>
https://appdividend.com/2019/02/25/javascript-error-object-tutorial-with-example/feed/ 0
Javascript Math Round Example | Math.round() Tutorial https://appdividend.com/2019/02/23/javascript-math-round-example-math-round-tutorial/ https://appdividend.com/2019/02/23/javascript-math-round-example-math-round-tutorial/#respond Sat, 23 Feb 2019 16:48:18 +0000 http://localhost/wordpress/?p=4943 Javascript Math Round Example Tutorial

Javascript Math Round Example | Math.round() Tutorial is today’s topic. The Math.round() function returns the value of a number rounded to the nearest integer. If the fractional portion of an argument is more than 0.5, the argument is turned to an integer with the next higher absolute value. If it is less than 0.5, then the argument […]

The post Javascript Math Round Example | Math.round() Tutorial appeared first on AppDividend.

]]>
Javascript Math Round Example Tutorial

Javascript Math Round Example | Math.round() Tutorial is today’s topic. The Math.round() function returns the value of a number rounded to the nearest integer. If the fractional portion of an argument is more than 0.5, the argument is turned to an integer with the next higher absolute value. If it is less than 0.5, then the argument is rounded to the integer with the lower value.  If the fractional portion is exactly 0.5, then the argument is rounded to the next integer in the direction of +∞.

Javascript Math Round Example

The syntax of Math.round() is following.

Math.round(x)

Where is a number. The value of a given number rounded to the nearest integer.

Let’s see the following example.

// app.js

console.log(Math.round(21.19));
console.log(Math.round(19.21));
console.log(Math.round(46.21));
console.log(Math.round(21.52));

The output is following.

Javascript Math Round Example | Math.round() Tutorial

The Math.round() function returns a value of the number rounded to the nearest integer.

If we want to round off a number to its nearest integer, the Math.round() function should be implemented.

The Math.round() function itself rounds off a negative number when passed as the parameter to it. See the following example.

// app.js

console.log(Math.round(-21.52));

The output is following.

Math.round() Tutorial

Finally, Javascript Math Round Example | Math.round() Tutorial is over.

The post Javascript Math Round Example | Math.round() Tutorial appeared first on AppDividend.

]]>
https://appdividend.com/2019/02/23/javascript-math-round-example-math-round-tutorial/feed/ 0