Criando o seu primeiro script - Curso JavaScript #04
Table of Contents
Introduction
This tutorial will guide you through the essentials of creating your first JavaScript script. You'll learn how to differentiate between HTML, CSS, and JavaScript, organize your project files in Visual Studio Code, check your Node.js installation, and use basic JavaScript commands like alert, confirm, and prompt. Following these steps will help you build a solid foundation in JavaScript programming.
Step 1: Set Up Your Project Structure
Proper organization of your project files is crucial for efficient coding and maintenance.
-
Create a Project Folder
- Choose a location on your computer.
- Name the folder (e.g.,
MeuProjetoJavaScript
).
-
Create Subfolders
- Inside your project folder, create the following subfolders:
html
css
js
- Inside your project folder, create the following subfolders:
-
Add Your Files
- Create an HTML file (e.g.,
index.html
) in thehtml
folder. - Create a CSS file (e.g.,
styles.css
) in thecss
folder. - Create a JavaScript file (e.g.,
script.js
) in thejs
folder.
- Create an HTML file (e.g.,
Step 2: Write Your First HTML File
Your HTML file is the backbone of your project, structuring the content that users will see.
-
Open
index.html
- Use Visual Studio Code to open your HTML file.
-
Basic HTML Structure
- Add the following code to your
index.html
file:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="../css/styles.css"> <title>Meu Primeiro Script</title> </head> <body> <h1>Bem-vindo ao Meu Primeiro Script!</h1> <script src="../js/script.js"></script> </body> </html>
- Add the following code to your
Step 3: Write Your First JavaScript Code
Now that you have your HTML set up, it's time to add some JavaScript functionality.
-
Open
script.js
- Access your JavaScript file in Visual Studio Code.
-
Add Basic Commands
- Write your first JavaScript code:
alert("Olá, mundo!");
-
Test Your Code
- Open
index.html
in a web browser. - You should see an alert box with the message "Olá, mundo!".
- Open
Step 4: Use confirm and prompt
Enhance user interaction with confirm
and prompt
commands.
-
Add a Confirm Command
- In your
script.js
, add the following code:
let userConfirmed = confirm("Você deseja continuar?"); if (userConfirmed) { alert("Você escolheu continuar!"); } else { alert("Você escolheu cancelar!"); }
- In your
-
Add a Prompt Command
- Next, implement a prompt to get user input:
let userName = prompt("Qual é o seu nome?"); alert("Bem-vindo, " + userName + "!");
-
Test Your Code Again
- Refresh your browser to see the changes in action.
Step 5: Check Node.js Installation
Before diving deeper into JavaScript, ensure that Node.js is installed correctly on your machine.
-
Open Terminal or Command Prompt
- Type the following command:
node -v
-
Verify Installation
- If Node.js is installed, you’ll see the version number.
- If not, download and install Node.js from nodejs.org.
Conclusion
Congratulations! You have successfully set up your first JavaScript script. You learned how to organize your project, create an HTML structure, and use JavaScript commands to interact with users. As a next step, consider exploring more complex JavaScript concepts and practicing coding exercises to solidify your understanding. Happy coding!