1.1 Introduction to JavaScript
What is JavaScript?
JavaScript is a versatile and powerful programming language that brings interactivity to web pages. It’s one of the core technologies of the web, alongside HTML and CSS, allowing developers to create dynamic and interactive user experiences.
History and Evolution
JavaScript was created by Brendan Eich in 1995 and has since evolved into a versatile language used for both client-side and server-side development. Its ecosystem includes powerful libraries and frameworks like React, Angular, and Vue.js.
Code Example: Displaying “Hello, World!”
console.log("Hello, World!");
This simple line of code prints “Hello, World!” to the console, using the console.log() function, which is essential for debugging and testing your code.
1.2 Setting Up Your Development Environment
Choosing a Text Editor or IDE
For writing JavaScript code, you can choose from several popular text editors and IDEs. Here are a few recommendations:
• Visual Studio Code: A highly popular, open-source editor with robust support for JavaScript.
• Sublime Text: Known for its speed and simplicity.
• Atom: Developed by GitHub, Atom is a hackable text editor great for collaboration.
Installing Node.js and npm
Node.js allows you to run JavaScript on the server side. Here’s a step-by-step guide to installing it:
1. Visit the Node.js website and download the installer for your operating system.
2. Run the installer and follow the instructions.
3. Verify the installation by typing the following commands in your terminal or command prompt:
node -v
npm -v
Using the Browser Console
Modern browsers like Chrome and Firefox come with built-in developer tools, which include a JavaScript console. You can open the console with Ctrl + Shift + J (Windows/Linux) or Cmd + Option + J (Mac) and use it to experiment with JavaScript code directly in your browser.
1.3 Basic Syntax and Structure
Variables and Data Types
In JavaScript, you can declare variables using let, const, or var. Here’s an example of each:
let name = "Alice"; // String
const age = 25; // Number
let isStudent = true; // Boolean
let scores = [85, 90, 78]; // Array
let person = { firstName: "John", lastName: "Doe" }; // Object
Control Structures
JavaScript supports conditional statements and loops to control the flow of your code.
• If Statements: Used to execute code based on conditions.
• Loops: Use for, while, and do...while loops to repeat code.
Code Example: If Statement and Loop
if (age >= 18) {
console.log("You are an adult.");
}
for (let i = 0; i < scores.length; i++) {
console.log("Score:", scores[i]);
}
FAQs
1. What is JavaScript used for?
JavaScript is used for adding interactivity to websites, creating web applications, and server-side programming.
2. Is JavaScript the same as Java?
No, JavaScript and Java are different languages with distinct uses and syntax.
3. Can JavaScript run outside the browser?
Yes, with Node.js, JavaScript can be used for server-side development.
One-Liner Questions
• What does console.log() do?
Outputs messages to the console.
• How do you declare a variable in JavaScript?
Use let, const, or var.
Coding Problems
1. Problem 1: Print Your Name
• Write a JavaScript program to print your name to the console.
console.log("Your Name");
2. Problem 2: Simple Calculator
• Create a program that adds two numbers and prints the result.
const num1 = 5;
const num2 = 10;
console.log(num1 + num2);
3. Problem 3: Variable Declaration
• Declare a variable to store your age and print it.
const age = 25;
console.log(age);
4. Problem 4: Check Even or Odd
• Write a function that checks if a number is even or odd.
function isEven(number) {
return number % 2 === 0;
}
console.log(isEven(4)); // true
Comments