Basic_js
Basic_js
1) what is Javascript?
Ans: JavaScript (js) is a lightweight object-oriented scripting language that is used by several websites for
scripting webpages. It is an interpreted, full-fledged programming language that enables dynamic
interactivity on websites when applied to an HTML document.
2) How many data types are used in Javascript?
Ans: Boolean - For true and false values
Null - For empty or unknown values
Undefined - For variables that are only declared and not defined or initialized
Number - For integer and floating-point numbers
String - For characters and alphanumeric values
Symbols - For unique identifiers for objects
Compound types:
Object - For collections or complex values
Array- Array is a collection of similar type of elements
3) What are some build in methods in javascript?
Ans: 1) Date() : Returns the present date and time
2) concat(): Joins two strings and returns the new string
3) push(): Adds an item to an array
4) pop(): Removes and also returns the last element of an array
5) round(): Rounds of the value to the nearest integer and then returns it
6) length(): Returns the length of a string
EXAMPLE :
console.log('Debugging message');
console.log(variableName);
2. Debugger Statement:
Insert the debugger; statement in your code where you want to break and start debugging. When the
browser encounters
this statement, it will pause execution, allowing you to inspect variables and step through the code.
EXAMPLE :
function myFunction() {
debugger;
// Your code here
}
7) What are the different ways an HTML element can be accessed in a JavaScript code?
Ans:
Here are the ways an HTML element can be accessed in a JavaScript code:
1) getElementByClass(‘classname’): Gets all the HTML elements that have the specified classname.
2) getElementById(‘idname’): Gets an HTML element by its ID name.
3) getElementbyTagName(‘tagname’): Gets all the HTML elements that have the specified tagname.
4) querySelector(): Takes CSS style selector and returns the first selected HTML element.
8) What is the difference between Undefined and Undeclared and null in JavaScript?
Ans:
1) Undefined : Undefined means a variable has been declared but a value has not yet been assigned to
that variable.
2) Undeclared : Variables that are not declared or that do not exist in a program or application.
3) Null : Null is an assignment value that we can assign to any variable that is meant to contain no value.
1. The BOM represents the interaction between the browser and the outside world, typically the user
and the operating system.
2. It includes objects such as window, which represents the browser window or tab, and navigator,
which provides information about the browser.
3. BOM is not standardized by W3C, and different browsers may have different implementations.
4. DOM (Document Object Model):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>BOM Example</title>
</head>
<body>
<script>
// Accessing the window object
console.log(window.innerWidth); // width of the browser window
</body>
</html>
The DOM represents the structured document as a tree of objects, where each object corresponds to a part
of the document, such as elements, attributes, and text.
It is a programming interface for web documents. It allows scripts to dynamically access and manipulate
the content, structure, and style of a document.
DOM is standardized by the W3C, making it consistent across different browsers.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>DOM Example</title>
</head>
<body>
<!-- A simple HTML structure -->
<div id="myDiv">
<p>Hello, <span id="mySpan">world</span>!</p>
</div>
<script>
// Accessing and manipulating the DOM
var myDiv = document.getElementById('myDiv'); // Accessing an element by ID
myDiv.style.backgroundColor = 'lightblue'; // Changing the background color
var mySpan = document.getElementById('mySpan');
mySpan.innerHTML = 'GPT-3'; // Changing the content of a span
// Creating a new element and appending it to the document
var newParagraph = document.createElement('p');
newParagraph.textContent = 'This is a new paragraph.';
document.body.appendChild(newParagraph);
</script>
</body>
</html>