Javascript | Operators | String Methods

Q.s 1 :- Create a javascript function to multiple 3 numbers & take input from the user (Prompt).
Q.s 2 :- Explain “==” & “===”.
Q.s 3 :- Explain use of “Slice” & “Trim” with an example.

1 Like
2. == is equal to operator & === is a strict equal to operator. If we take two 
numbers same one as a string and other as a number, == will give true whereas === will 
give false.  this implies == compares data whereas === compares both data & data type.
3. Trim is used to remove whitespaces in a value. 
ex- var str= "   hello  ";
document.write(str.trim());
it will remove spaces before and after spaces of hello
Slice is used for dividing a string. We can do slicing by using negative indexing also 
ex- var str= " hello world";
document.write(str.slice(6,10));
document.write(str.slice(-10,-6));

2 Likes

Heres a JavaScript function that multipliees three numbers, taking input from the user using the prompt() function

function multiplyNumbers() {
  var num1 = Number(prompt("Enter the first number:"));
  var num2 = Number(prompt("Enter the second number:"));
  var num3 = Number(prompt("Enter the third number:"));

  var result = num1 * num2 * num3;

  alert("The result is: " + result);
}

2.In JavaScript, “==” and “===” are comparison operators used to compare values. Here’s how they differ

== (loose equality): This operator compares values after performing type coercion, if necessary. It attempts to convert the operands to the same type before making the comparison
Example

`10 == "10" // true
`

this case, the string “10” is converted to the number 10 before the comparison is made.

=== (strict equality): This operator compares values without performing type coercion. It checks both the value and the type of the operands.

Example:

10 === "10" // false

Since the value on the left is a number and the value on the right is a string, the comparison evaluates to false
generally recommend to use === for equality comparisons in JavaScript because it avoids unexpected behavior that can arise from type coercion

3.slice() is a string method in JavaScript that returns a portion of a string. It takes two optional parameters: the starting index (inclusive) and the ending index (exclusive). It does not modify the original string

Example:

var str = "Hello, World!";
var slicedStr = str.slice(7, 12);
console.log(slicedStr); // "World"

this example, slice(7, 12) retursnthe substring “World” from the original string.
trim() is a string method that removes whitespace from both ends of a string. It does not modify the original string but returns a new string.

Example:

var str = "   Hello, World!   ";
var trimmedStr = str.trim();
console.log(trimmedStr); // "Hello, World!"

trim() function removes the leading and trailing whitespace from the string " Hello, World! " and returns the trimmed string “Hello, World!”
string methodds are helpful for manipulating and cleaning up strings in JavaScript.

3 Likes
  1. var a=10;
    var b=5;
    var c=5;
    document.write(abc);
  2. == :-It is check for data is equal or not , with
    casesensitive.
    === :- It is check for data as well as datatype
    3)trim :- It is used for deleting space
    var a=" A “;
    var b=” B ";
    var c=a.trim()+b.trim();
    (about slice, i have no idea)
1 Like

== it is used for check data is equal or not…if data is equal then ans come true otherwise false.
=== it is also used for check equal or not but it check stricctly.
trim is used for giving space …

1 Like

Ans 1.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        function funName(){
            let inputOne=parseInt(prompt('Enter First Number'));
            let inputTwo=parseInt(prompt('Enter Second Number'));
            let inputThree=parseInt(prompt('Enter Third Number'));
            let add=inputOne*inputTwo*inputThree;
            document.write("First Number = "+inputOne+"<br>Second Number = "+inputTwo+"<br>Second Number = "+inputThree+"<br>Total = "+add);
        }

    </script>
</head>
<body>
    <button type="button" onclick="funName()">Run</button>
</body>
</html>

Ans 2: == is condition check operator. Its check data is equal or not but === check data type and data.

Ans3: The slice() method extracts a part of a string.
var string = “Hello, Friends!”;
var slicedString = string.slice(3, 6);
document.write(slicedString);

The trim() method removes whitespace from both sides of a string.
var trim = " Hello! ";
var trimString = string.trim();
document.write(slicedString);

1 Like

Ans: 1

function multiplyThreeNumbers() {
var number1 = parseFloat(prompt(“Enter the first number:”));
var number2 = parseFloat(prompt(“Enter the second number:”));
var number3 = parseFloat(prompt(“Enter the third number:”));

var result = number1 * number2 * number3;

alert("The result of multiplying the three numbers is: " + result);
}

Ans: 2
Double Equals (==):It compares the values for equality after performing type coercion if necessary.
Ex: console.log(5 == ‘5’); // true

Triple Equals (===): It checks both the values and the types of the operands.
Ex: console.log(5 === ‘5’); // false

Ans 3:
Slice: The slice() method is used to extract a portion of a string or an array
Ex:var str = “Hello, World!”;
var slicedStr = str.slice(7, 12);
console.log(slicedStr); // Output: World

Trim: The trim() method removes whitespace from both ends of a string.
Ex: var str = " Hello, World! ";
var trimmedStr = str.trim();
console.log(trimmedStr); // Output: “Hello, World!”

1.function multiply() {

var a= prompt("Enter 1st number :");
var b= prompt("Enter 2nd number :");
var c= prompt("Enter 3rd number :");

var ans= a*b*c;
alert(ans);
}

multiply();

Ans 2: Double equals (==) is referred to as loose equality because it performs type coercion before making any comparison. where triple equals (===), also referred to as strict equality, works similarly to how double equals (==) works, with one important difference it does not convert the types of the operands before comparing.

Ans 3: The slice() method returns a shallow copy of a portion of an array into a new array object.
var numbers = [2, 3, 5, 7, 11, 13, 17];
var newArray = numbers.slice(3, 6);
console.log(newArray);
Ans:1
String trim() method is used to remove the white spaces from both ends of the given string.
var str = " JsforJavascript"
var st = str.trim()
console.log(st)

on 1st answer you can call that function to get the output.

1st answer you can call that function to get the output.