JavaScript is a versatile, high-level programming language primarily used to create interactive and dynamic features on web pages. It is a core technology of the web, alongside HTML and CSS, and is integral to modern web development. Here’s an overview of JavaScript:
var
, let
, or const
.javascript let name = "John"; const age = 30;
Number
, String
, Boolean
, Object
, Array
, Null
, Undefined
, and Symbol
.javascript let number = 42; // Number let text = "Hello"; // String let isActive = true; // Boolean
+
, -
, *
, /
), comparison (==
, ===
, !=
, >
), and logical (&&
, ||
, !
).javascript let result = 5 + 3; // 8 let isEqual = (5 === 5); // true
javascript function greet(name) { return `Hello, ${name}!`; }
javascript const greet = function(name) { return `Hello, ${name}!`; };
javascript const greet = (name) => `Hello, ${name}!`;
if
, else
, switch
javascript if (age > 18) { console.log("Adult"); } else { console.log("Minor"); }
for
, while
, do...while
javascript for (let i = 0; i < 5; i++) { console.log(i); }
javascript const person = { name: "Alice", age: 25, greet: function() { return `Hello, ${this.name}!`; } };
javascript const fruits = ["apple", "banana", "cherry"]; console.log(fruits[1]); // banana
getElementById
:javascript const element = document.getElementById("my-id");
querySelector
:javascript const element = document.querySelector(".my-class");
javascript element.innerHTML = "New Content";
javascript element.style.color = "blue";
javascript element.addEventListener("click", function() { alert("Element clicked!"); });
javascript function fetchData(callback) { setTimeout(() => { callback("Data received"); }, 1000); } fetchData((data) => console.log(data));
javascript let promise = new Promise((resolve, reject) => { setTimeout(() => resolve("Data received"), 1000); }); promise.then(data => console.log(data));
javascript async function fetchData() { let response = await fetch("https://api.example.com/data"); let data = await response.json(); console.log(data); } fetchData();
`
), allowing for embedded expressions.javascript let name = "Bob"; let greeting = `Hello, ${name}!`;
javascript const [a, b] = [1, 2]; const {name, age} = {name: "Alice", age: 25};
javascript // In file math.js export function add(x, y) { return x + y; } // In file app.js import { add } from './math.js';
try {
// Code that may throw an error
let result = riskyFunction();
} catch (error) {
console.error("An error occurred:", error);
}
console.log()
: Print messages to the console.javascript console.log("Debug message");
JavaScript is essential for creating interactive and responsive web applications, and its ecosystem continues to evolve with new features and tools that enhance its capabilities.
CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation and design…
HTML (HyperText Markup Language) is the foundational language for creating and structuring web pages and…
Front-end development focuses on creating the visual and interactive aspects of a website or web…
Modern physics is a broad field encompassing several sub-disciplines, including quantum mechanics, relativity, particle physics,…
This website uses cookies.