JavaScript Basics - B.
Tech Semester Notes
1. What are JavaScript Data Types?
JavaScript data types are divided into two categories:
1. Primitive Data Types:
- String: Text data (e.g., let name = "Alice";)
- Number: Numeric data (e.g., let age = 25;)
- BigInt: Large integers (e.g., let bigNum = 12345678901234567890n;)
- Boolean: true or false (e.g., let isStudent = true;)
- Undefined: Declared but not assigned (e.g., let x;)
- Null: Represents no value (e.g., let value = null;)
- Symbol: Unique identifiers (e.g., let sym = Symbol("id");)
2. Non-Primitive (Reference) Data Types:
- Object: Key-value pairs (e.g., let person = { name: "John", age: 22 };)
- Array: Ordered collection (e.g., let marks = [85, 90, 95];)
- Function: Callable block (e.g., function greet() { console.log("Hello"); })
2. How can you create an object in JavaScript with a simple example?
You can create an object using object literal syntax:
Example:
let student = {
name: "Ravi",
rollNo: 101,
department: "CSE"
};
Accessing properties:
console.log(student.name); // Ravi
3. What is DOM and how is it involved in an HTML webpage?
DOM (Document Object Model) is a tree-like structure created by the browser from the HTML
document.
- Each HTML element is represented as a node.
- JavaScript can use the DOM to change content, styles, and structure.
Example:
<p id="demo">Original Text</p>
<script>
document.getElementById("demo").innerText = "Changed Text";
</script>
4. How to validate email and mobile number using regular expressions in JavaScript?
Email Validation:
function validateEmail(email) {
const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return regex.test(email);
}
Mobile Number Validation (10 digits, optional country code):
function validateMobile(number) {
const regex = /^(\+?\d{1,3}[- ]?)?\d{10}$/;
return regex.test(number);