Home > Web Front-end > JS Tutorial > body text

JavaScript in inute

WBOY
Release: 2024-08-06 00:39:22
Original
801 people have browsed it

JavaScript in inute

Introduction

JavaScript is a versatile, high-level programming language commonly used in web development to create dynamic and interactive user experiences. It can be used for front-end and back-end development, making it an essential tool for modern web developers.

Setting Up

  1. Browser Console: Open your web browser and access the console (usually found in Developer Tools).
  2. Text Editor: Use any text editor like VSCode, Sublime Text, or even Notepad.

Basics

Variables

Variables are used to store data. You can declare variables using let, const, or var.

let name = "John";
const age = 30;
var isStudent = true;
Copy after login

Data Types

JavaScript supports various data types, including:

  • String: "Hello"
  • Number: 42
  • Boolean: true or false
  • Array: [1, 2, 3]
  • Object: { key: "value" }
  • Undefined: undefined
  • Null: null

Functions

Functions are blocks of code designed to perform a particular task.

function greet() {
  console.log("Hello, World!");
}

greet(); // Output: Hello, World!
Copy after login

Control Structures

Conditional Statements

Used to perform different actions based on different conditions.

let time = 20;
if (time < 12) {
  console.log("Good morning");
} else if (time < 18) {
  console.log("Good afternoon");
} else {
  console.log("Good evening");
}
Copy after login

Loops

Used to repeat a block of code a number of times.

for (let i = 0; i < 5; i++) {
  console.log(i); // Output: 0 1 2 3 4
}
Copy after login

Events

JavaScript can respond to events, like clicks or keyboard input.

<button onclick="sayHello()">Click me</button>
<script>
  function sayHello() {
    alert("Hello!");
  }
</script>
Copy after login

DOM Manipulation

JavaScript can interact with HTML elements to change content, style, and attributes.

<p id="demo">This is a paragraph.</p>
<button onclick="changeText()">Change Text</button>
<script>
  function changeText() {
    document.getElementById("demo").innerHTML = "Text changed!";
  }
</script>
Copy after login

Arrays and Objects

Arrays and objects are used to store collections of data.

Arrays

let fruits = ["Apple", "Banana", "Cherry"];
console.log(fruits[0]); // Output: Apple
Copy after login

Objects

let person = {
  firstName: "John",
  lastName: "Doe",
  age: 30
};
console.log(person.firstName); // Output: John
Copy after login

ES6 Features

Modern JavaScript (ES6 and later) introduces several new features:

Arrow Functions

const add = (a, b) => a + b;
console.log(add(2, 3)); // Output: 5
Copy after login

Template Literals

let name = "John";
console.log(`Hello, ${name}!`); // Output: Hello, John!
Copy after login

Destructuring

let person = { firstName: "John", lastName: "Doe" };
let { firstName, lastName } = person;
console.log(firstName); // Output: John
Copy after login

Conclusion

This is just a brief introduction to JavaScript. There's much more to explore, including advanced topics like asynchronous programming and API's ......

I’m excited to share more blog posts like this. just follow and give reactions

The above is the detailed content of JavaScript in inute. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!