1. null:
A variable explicitly assigned with no value. It represents "nothing" or "no value".
let y = null;
console.log(y); // null
Use null when you want to intentionally indicate "no value".
2. undefined:
A variable has been declared but has not been assigned a value.
Use undefined when something is not yet assigned.
let variable = undefined;
// or
let x;
Literals
Literals in JavaScript are fixed values that are directly written into the code. They represent data values such as numbers, strings, booleans, arrays, objects, and functions. Unlike variables, literals do not change their values during program execution.
Template Literals
Template literals are string literals that allow embedded expressions (variables) into your code. They are enclosed by backticks (`) instead of single (') or double (") quotes.
Syntax
string text ${expression} string text
Backticks (): Used to define a template literal.
${}: Used to insert expressions inside the string, such as variables, function calls, or arithmetic operations.
Example
hello ${a}
let a='GFG'
console.log()
Output:
hello GFG
Create a Element in Javascript:
To create an element in JavaScript, you use the document. createElement() method. This is part of the DOM (Document Object Model) and is how you dynamically add HTML elements using JavaScript.
Basic Syntax:
let element = document.createElement("tagName");
"tagName" is the type of element you want to create, like "div", "p", "button", etc.
Example: Create a
element and add it to the page
`
// 1. Create the element
const paragraph = document.createElement("p");
// 2. Add some text to it
paragraph.textContent = "Hello, this is a new paragraph!";
// 3. Add it to the body (or any other part of the page)
document.body.appendChild(paragraph);
`
**
- document.createElement("tag")
- Set textContent, innerHTML, or attributes
- Add it to the DOM with appendChild() or append() **
Remove a Element in Javascript:
let element = document.getElementById("myElement").remove();
Top comments (0)