What is JavaScript?
JavaScript (JS) is a versatile, high-level programming language primarily used for creating interactive web pages. It’s an essential part of web development alongside HTML and CSS.
Why Learn JavaScript?
- Ubiquity: It’s supported by all modern web browsers.
- Versatility: Used for front-end, back-end, mobile, and desktop development.
- Large Community: Extensive resources and libraries available.
Setting Up Your Environment
To start coding in JavaScript, you only need:
- A text editor (e.g., Visual Studio Code, Sublime Text)
- A web browser (e.g., Chrome, Firefox)
Your First JavaScript Program
Let’s write a simple “Hello, World!” program:
console.log("Hello, World!");
To run this:
- Open your text editor.
- Type the code above.
- Save the file as
hello.js
. - Open your web browser’s console (usually by pressing F12).
- Copy and paste the code into the console and press Enter.
You should see “Hello, World!” printed in the console.
Basic Syntax
Variables
Variables are used to store data. In modern JavaScript, we use let
and const
:
let name = "John";
const age = 30;
Data Types
JavaScript has several basic data types:
- String: Text
let greeting = "Hello";
- Number: Integers and floating-point numbers
let count = 42;
let price = 19.99;
- Boolean: True or false
let isActive = true;
- Undefined: Variable declared but not assigned a value
let unknownValue;
- Null: Intentional absence of any object value
let emptyValue = null;
Basic Operators
- Arithmetic:
+
,-
,*
,/
,%
- Comparison:
==
,===
,!=
,!==
,>
,<
,>=
,<=
- Logical:
&&
(AND),||
(OR),!
(NOT)
Conditional Statements
Use if
, else if
, and else
for decision making:
let hour = 14;
if (hour < 12) {
console.log("Good morning!");
} else if (hour < 18) {
console.log("Good afternoon!");
} else {
console.log("Good evening!");
}
Practice Exercise
Try creating a variable with your name and use it in a greeting:
let yourName = "Your Name Here";
console.log("Hello, " + yourName + "! Welcome to JavaScript.");
Conclusion
This lesson covered the very basics of JavaScript. In the next lesson, we’ll dive deeper into functions, arrays, and objects.
Remember, practice is key in programming. Experiment with the concepts we’ve covered and don’t hesitate to explore further!