Write a js code to find a factorial of a given number?
<!DOCTYPE html>
<html>
<body>
<script>
function factorial(n) {
if (n == 0)|| (n==1)
return 1;
else
return n * factorial(n – 1);
}
let num = 5;
document.write(“Factorial of a given number is ” + factorial(num));
</script>
</body>
</html>
Write a function to enter two numbers and find the sum of two numbers in javascript.
<!DOCTYPE html>
<html>
<body>
<script>
function sum(a, b) {
return a + b;
}
let num1 = parseInt(prompt(“Enter a value: “));
let num2 = parseInt(prompt(“Enter another value: “));
let add=sum(num1,num2);
document.write(“Sum is ” +add);
</script>
</body>
</html>
Difference between server-side and client side scripting?
| Server-Side Scripting | Client-Side Scripting |
|---|---|
| Runs on the server computer | Runs on your browser (your computer) |
| Example: PHP, Python | Example: JavaScript |
| User cannot see the code | User can see the code |
| Can work with databases | Cannot work with databases |
| Makes web pages ready for user | Changes web page after it shows up |
| Slower because server does the work | Faster because your computer does the work |
| Used for back-end. | Used for front-end. |
| More secure | Less secure |
Explain different tpes of data types used n javascript
Datatype refers to the type of data.
Types of Data Types in JavaScript:
Number – Used to store numeric values (e.g., 10, 3.14)
let x=10;
let y=10.5;
String – Used to store text inside quotes (e.g., “Hello”, ‘JS’)
let name=”rajesh”;
let program=’javascript’;
Boolean – Stores only two values: true or false
let isStudent = true;
let hasLogin = false;
Undefined – A variable that is declared but has no value yet
let x;
Null – Represents an empty value
let abc=null;
Object – Stores data in key-value pairs (e.g., {name: “Alice”, age: 20})
let person = {name: “Bob”, age: 25};
Array – A type of object that stores multiple values in a list (e.g., [1,2,3])
let fruits = [“Apple”, “Banana”, “Mango”];