In JavaScript, variables are used to store data values. You can declare variables using var
, let
, or const
.
// Using var
var name = 'John';
// Using let
let age = 30;
// Using const for a constant value
const PI = 3.14;
JavaScript has several data types, including primitive data types and reference data types.
number
, string
, boolean
, null
, undefined
, symbol
let num = 10;
let str = "Hello";
let isTrue = true;
let empty = null;
let notDefined;
let sym = Symbol('description');
object
, array
, function
// Object
let person = {
name: 'Alice',
age: 25
};
// Array
let numbers = [1, 2, 3];
// Function
function add(a, b) {
return a + b;
}
JavaScript has control structures like if - else
, switch
, for
, while
, and do - while
loops.
// if - else
let score = 80;
if (score >= 60) {
console.log('Pass');
} else {
console.log('Fail');
}
// for loop
for (let i = 0; i < 5; i++) {
console.log(i);
}
You can include JavaScript in an HTML file in two ways:
<script>
tag.<!DOCTYPE html>
<html>
<body>
<button onclick="alert('Hello!')">Click me</button>
<script>
function sayHello() {
console.log('Hello from inline script');
}
sayHello();
</script>
</body>
</html>
.js
file and link it to the HTML file.<!DOCTYPE html>
<html>
<body>
<script src="script.js"></script>
</body>
</html>
In script.js
:
console.log('This is an external script');
Node.js allows you to run JavaScript outside of the browser. Here is a simple example of a Node.js server:
const http = require('http');
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content - Type', 'text/plain');
res.end('Hello, World!\n');
});
server.listen(3000, '127.0.0.1', () => {
console.log('Server running at http://127.0.0.1:3000/');
});
In web development, event handling is a common practice. For example, handling a click event on a button.
<!DOCTYPE html>
<html>
<body>
<button id="myButton">Click me</button>
<script>
const button = document.getElementById('myButton');
button.addEventListener('click', function () {
alert('Button clicked!');
});
</script>
</body>
</html>
The Document Object Model (DOM) represents the HTML document as a tree - like structure. You can use JavaScript to manipulate the DOM.
<!DOCTYPE html>
<html>
<body>
<p id="myParagraph">Original text</p>
<script>
const paragraph = document.getElementById('myParagraph');
paragraph.textContent = 'New text';
</script>
</body>
</html>
let
and const
Instead of var
let
and const
have block - scope, which makes the code more predictable and less error - prone compared to var
.
Use a consistent coding style. Tools like ESLint can help you enforce a coding style. For example, you can use the Airbnb JavaScript Style Guide.
Use try - catch
blocks to handle errors gracefully.
try {
let result = 1 / 0;
if (isNaN(result)) {
throw new Error('Division by zero');
}
} catch (error) {
console.error('An error occurred:', error.message);
}
JavaScript is a versatile and powerful programming language that is essential for modern web development. By understanding its fundamental concepts, usage methods, common practices, and best practices, you can write more efficient, reliable, and maintainable JavaScript code. Whether you are building a simple web page or a complex server - side application, JavaScript has the tools and capabilities to meet your needs.