5. String and Math Object in JavaScript

 String in JavaScript 

- A string in JavaScript is a sequence of Unicode characters. It is simply an array of characters. Each character takes 16 bits.

- Each character in the string has its position or index. 


Ex :

let name1 = "John"; // Double quotes

let name2 = 'Bob'; // Single quotes.


String Object in JavaScript

- String is an object. Any variable whose value is a string is actually a string object. That is, JavaScript stores a string value as a string object in a variable.

- The string object in JavaScript is a wrapper around the string primitive value. It is the most commonly used object in JavaScript.


String Creation 

Ways to create String in JavaScript

There are two ways to create string in JavaScript. They are as follows:

1. By string literal 

Ex : let stringname="string value";


2. By string object (using new keyword and String() constructor)

Ex : let str = new String("Hello JavaScript");


- We can also using character array

 


 Note: 

- Although, creating a string in JavaScript using new keyword is not recommended because it consumes more memory and slow down the execution speed. Strings objects may produce unexpected result. 

-  The String object in JavaScript has only three properties: length, prototype, and constructor. We can use length property with both string literals and string objects.But, to use the properties prototype and constructor, we need string objects. That is, we cannot use the prototype and constructor properties with string literals.


Math Object  in JavaScript
- Math in JavaScript is a top-level, pre-defined object that has properties and methods for mathematical constants and functions.

Syntax to Access Elements of Math Object
- To access the properties and functions of Math object, we do not need to create an instance.
- We can access properties and functions of JavaScript Math, as usual, by placing a dot after the object name (Math) and writing the property or function name.

Syntax : 
Math.property_name;
Math.function_name();

For example:
   var pi = Math.PI;
   var p = Math.pow();


Static Properties of JavaScript Math object
All the properties refer to constants that do not change. Therefore, these properties are read-only properties.
(a) Math.E: It is Euler’s constant and the base of natural logarithms.
(b) Math.LN2: It is the natural logarithm of 2, approximate value o.693. 
(c) Math.LN10: It is a natural logarithm of 10, approximate value 2.302.
(d) Math.LOG2E: Another important constant in math is base-2 logarithm of Euler’s constant. Its approximate value is 1.442. 
(e) Math..LOG10E: It is a base-10 logarithm of Euler’s constant. Its approximate value is 0.434.
(f) Math.PI: It returns the value of pi, that is the ratio of circumference of a circle to its diameter. Its approximate value is 3.14. 
(g) Math.SQRT2: It is a square root of 2 that is a well-known constant in mathematics. Its approximate value is 1.414. 
(h) Math.SQRT1_2: It is the square root of 0.5, whose approximate value is 0.707.

Static Methods of Math object

We can divide the methods of Math object in JavaScript into four categories. Each one is related to a distinct branch of mathematics. They are:

1. Trigonometry methods
    - a. Math.sin(x): Returns the sine of a number.
    - b. Math.cos(x): Returns the cosine of a number.
    - c. Math.tan(x): Returns the tangent of a number.


2. Integer methods
    - a. Math.floor(x): Returns the largest integer less than or equal to a given number.
    - b. Math.ceil(x) : Returns the smallest integer greater than or equal to a given number.
    - c. Math.round(x): Returns the value of a number rounded to the nearest integer.

3. Logarithmic, Exponential, Power, Root methods
    - a. Math.log(x): Returns the natural logarithm (base e) of a number.
    - b. Math.exp(x): Returns the value of E raised to the power of a given number.
    - c. Math.pow(x, y): Returns the value of x to the power of y.
    - d. Math.sqrt(x): Returns the square root of a number.


4. Miscellaneous methods
    - a. Math.abs(x): Returns the absolute value of a number.
    - b. Math.random(): Returns a random floating-point number between 0 (inclusive) and 1 (exclusive).

Comments

Popular posts from this blog

1. Introduction of Javascript

2. Datatypes & Variables in Javascript

6. Object and Array in Javascript