Variables:
- Variables are used to store data values.
- In JavaScript, you can declare variables using the
var
,let
, orconst
keywords. - Variables declared with
var
have function scope, while those declared withlet
orconst
have block scope (scoped to the nearest enclosing block). const
declares a constant variable whose value cannot be reassigned.
Data Types:
- JavaScript has several primitive data types, including numbers, strings, booleans, null, undefined, and symbols (introduced in ES6).
- Objects and arrays are complex data types.
- JavaScript is dynamically typed, meaning you don't need to explicitly declare the data type of a variable.
Operators:
- JavaScript supports various operators for performing operations on variables and values.
- Arithmetic operators (
+
,-
,*
,/
,%
) perform basic mathematical operations. - Comparison operators (
==
,!=
,===
,!==
,>
,<
,>=
,<=
) compare values and return a boolean result. - Logical operators (
&&
,||
,!
) perform logical operations on boolean values.
Strings:
- Strings are sequences of characters enclosed in single or double quotes.
- JavaScript provides various methods and properties for working with strings, such as
length
,charAt()
,substring()
,toUpperCase()
,toLowerCase()
, etc.
Conditional Statements:
- Conditional statements allow you to execute different blocks of code based on different conditions.
- The
if...else
statement evaluates a condition and executes a block of code if the condition is true, otherwise, it executes an alternative block of code. - The
switch
statement evaluates an expression and executes a block of code based on matching cases.
Loops:
- Loops are used to execute a block of code repeatedly until a certain condition is met.
- The
for
loop repeats a block of code a specified number of times. - The
while
loop repeats a block of code while a specified condition is true. - The
do...while
loop repeats a block of code once, and then repeats the block while a specified condition is true.
Functions:
- Functions are blocks of reusable code that perform a specific task.
- Functions can take parameters (input) and return a value (output).
- JavaScript functions can be declared using the
function
keyword or as arrow functions (introduced in ES6).
Arrays:
- Arrays are ordered collections of data stored in a single variable.
- JavaScript arrays can contain elements of different data types and can be dynamically resized.
- Arrays have properties and methods for accessing and manipulating their elements.
Objects:
- Objects are collections of key-value pairs where each key is a string (or symbol) and each value can be any data type, including other objects or functions.
- Objects can represent real-world entities and have properties (attributes) and methods (functions).
Scope:
- Scope refers to the visibility of variables in different parts of your code.
- JavaScript has function scope and block scope, depending on how variables are declared (
var
,let
,const
).
These basic concepts provide the foundation for understanding and writing JavaScript code. As you continue your learning journey, you'll encounter more advanced topics and techniques for building powerful applications with JavaScript.
Here's an overview of the basic syntax of JavaScript:
Comments:
// This is a single-line comment /* This is a multi-line comment */
Variables:
var x = 10; // Declaring a variable (global scope) let y = 20; // Block-scoped variable (introduced in ES6) const z = 30; // Constant (block-scoped variable with a fixed value)
Data Types:
let num = 10; // Number let str = "Hello"; // String let bool = true; // Boolean let arr = [1, 2, 3]; // Array let obj = { // Object key: "value" }; let n = null; // Null let u = undefined; // Undefined
Operators:
let sum = 2 + 3; // Addition let difference = 5 - 2; // Subtraction let product = 4 * 6; // Multiplication let quotient = 8 / 2; // Division let remainder = 10 % 3; // Modulus
Strings:
let str1 = "Hello"; let str2 = 'world'; let combined = str1 + ' ' + str2; // Concatenation
Conditional Statements:
if (condition) { // code block } else if (anotherCondition) { // another code block } else { // fallback code block }
Loops:
- for loop:
- for (let i = 0; i < 5; i++) { // code block }
- while loop:
- let i = 0; while (i < 5) { // code block i++; }
- do...while loop:
- let i = 0; do { // code block i++; } while (i < 5);
Functions:
function greet(name) { return "Hello, " + name + "!"; } let message = greet("Alice"); // Function call
Arrays:
let fruits = ["Apple", "Banana", "Orange"]; let firstFruit = fruits[0]; // Accessing elements fruits.push("Mango"); // Adding an element
Objects:
let person = { name: "John", age: 30, isStudent: false }; let personName = person.name; // Accessing properties person.city = "New York"; // Adding a property
Scope:
- JavaScript has function scope (variables declared within a function are only accessible within that function) and block scope (variables declared within a block like
if
orfor
are only accessible within that block, if usinglet
orconst
).
- JavaScript has function scope (variables declared within a function are only accessible within that function) and block scope (variables declared within a block like
Comments:
- Comments are used to explain code and are ignored by JavaScript when executing. They can be single-line (
//
) or multi-line (/* */
).
- Comments are used to explain code and are ignored by JavaScript when executing. They can be single-line (
This is a foundational overview of JavaScript syntax. As you delve deeper into JavaScript programming, you'll encounter more advanced concepts and syntax, but these basics will give you a solid starting point.
0 Comments