![]() | ㅤ | Tamim Ahmed17 Mar 2023 (1 week ago)
|

JavaScript is a programming language that is widely used for creating interactive web pages and web applications. Here are some key features of its syntax:
Variables: Variables are used to store values in JavaScript. They are declared using the var
, let
, or const
keywords, followed by the variable name and an optional initial value.
var x = 10;
let y = 5;
const PI = 3.14;
Data types: JavaScript has several data types, including numbers, strings, booleans, objects, and arrays.
let num = 10;
let str = "Hello";
let bool = true;
let obj = {name: "John", age: 30};
let arr = [1, 2, 3];
Operators: JavaScript supports various operators, including arithmetic, comparison, logical, and assignment operators.
let a = 10;
let b = 5;
let c = a + b; // addition operator
let d = (a > b) ? "a is greater than b" : "b is greater than a"; // ternary operator
let e = true && false; // logical AND operator
let f += 5; // addition assignment operator
Conditional statements: JavaScript supports conditional statements such as if
, else if
, and else
, which are used to execute different code blocks based on certain conditions.
let age = 18;
if (age < 18) {
console.log("You are too young to vote");
} else if (age >= 18 && age < 65) {
console.log("You are eligible to vote");
} else {
console.log("You are too old to vote");
}
Loops: JavaScript supports different types of loops, including for
, while
, and do-while
, which are used to repeat a block of code multiple times.
for (let i = 0; i < 5; i++) {
console.log(i);
}
let j = 0;
while (j < 5) {
console.log(j);
j++;
}
let k = 0;
do {
console.log(k);
k++;
} while (k < 5);
These are just some of the features of JavaScript syntax. JavaScript is a rich and flexible language, and there are many other features and capabilities that you can explore as you learn more about it.
JavaScript Values
In JavaScript, values are used to represent various types of data. Here are some of the most common types of values in JavaScript:
- Numbers: Numbers are used to represent numeric values in JavaScript. They can be either integers or floating-point numbers.
let x = 5; // integer
let y = 3.14; // floating-point number
- Strings: Strings are used to represent text in JavaScript. They are enclosed in single or double quotes.
let str1 = 'Hello';
let str2 = "world";
- Booleans: Booleans are used to represent true or false values in JavaScript.
let bool1 = true;
let bool2 = false;
- Undefined: Undefined is a special value that represents the absence of a value. It is often used to indicate that a variable has not been assigned a value yet.
let x;
console.log(x); // undefined
- Null: Null is another special value that represents the absence of a value. It is often used to indicate that a variable intentionally has no value.
let x = null;
console.log(x); // null
- Objects: Objects are used to represent complex data structures in JavaScript. They are collections of key-value pairs, where the keys are strings and the values can be any type of value.
let person = {
name: "John",
age: 30,
hobbies: ["reading", "traveling"],
address: {
street: "123 Main St",
city: "New York",
state: "NY"
}
};
- Arrays: Arrays are used to represent lists of values in JavaScript. They are ordered collections of values, which can be of any type.
let arr = [1, "two", true, null];
These are just some of the types of values that can be used in JavaScript. There are other types of values as well, such as functions, symbols, and dates, which can be useful for more advanced programming tasks.
JavaScript Variables
In JavaScript, variables are used to store and manipulate data. A variable is a container that holds a value, which can be of any type. Here’s how you can declare a variable in JavaScript:
let variableName;
This creates a variable called variableName
without assigning any value to it. To assign a value to the variable, you can use the assignment operator =
:
variableName = 5;
You can also declare and assign a value to a variable in a single statement:
let variableName = 5;
JavaScript uses dynamic typing, which means that the type of a variable can change at runtime. For example, you can assign a string value to a variable that previously held a numeric value:
let variableName = 5;
variableName = "Hello";
JavaScript has several types of variables:
let
: This is used to declare a variable that can be reassigned a new value. It has a block scope, which means that it is only accessible within the block of code where it is declared.
let x = 5;
x = 10; // valid
const
: This is used to declare a variable that cannot be reassigned a new value. It also has a block scope.
const y = 5;
y = 10; // invalid
var
: This is an older way of declaring variables in JavaScript. It has a function scope, which means that it is accessible throughout the entire function where it is declared. It can also be reassigned a new value.
var z = 5;
z = 10; // valid
In general, it is recommended to use let
and const
instead of var
, as they provide better scoping and prevent some common programming errors.
JavaScript Operators
Operators in JavaScript are used to perform operations on values or variables. JavaScript has a variety of operators that can be used for different purposes. Here are some of the most common types of operators in JavaScript:
- Arithmetic Operators: Arithmetic operators are used to perform mathematical operations on numbers. Some of the most common arithmetic operators are:
+
(Addition)-
(Subtraction)*
(Multiplication)/
(Division)%
(Modulus)++
(Increment)--
(Decrement)
let a = 5;
let b = 2;
console.log(a + b); // 7
console.log(a - b); // 3
console.log(a * b); // 10
console.log(a / b); // 2.5
console.log(a % b); // 1
console.log(++a); // 6
console.log(--b); // 1
- Assignment Operators: Assignment operators are used to assign values to variables. Some of the most common assignment operators are:
=
(Assignment)+=
(Add and assign)-=
(Subtract and assign)*=
(Multiply and assign)/=
(Divide and assign)%=
(Modulus and assign)
let a = 5;
a += 2; // equivalent to a = a + 2
console.log(a); // 7
a -= 3; // equivalent to a = a - 3
console.log(a); // 4
a *= 2; // equivalent to a = a * 2
console.log(a); // 8
a /= 4; // equivalent to a = a / 4
console.log(a); // 2
a %= 3; // equivalent to a = a % 3
console.log(a); // 2
- Comparison Operators: Comparison operators are used to compare two values and return a Boolean value (true or false). Some of the most common comparison operators are:
==
(Equal to)!=
(Not equal to)>
(Greater than)<
(Less than)>=
(Greater than or equal to)<=
(Less than or equal to)
let a = 5;
let b = 2;
console.log(a == b); // false
console.log(a != b); // true
console.log(a > b); // true
console.log(a < b); // false
console.log(a >= b); // true
console.log(a <= b); // false
- Logical Operators: Logical operators are used to combine multiple Boolean values and return a Boolean value. Some of the most common logical operators are:
&&
(Logical AND)||
(Logical OR)!
(Logical NOT)
let a = 5;
let b = 2;
let c = 8;
console.log((a > b) && (b < c)); // true
console.log((a > b) || (b > c)); // true
console.log(!(a > b)); // false
These are some of the most common types of operators in JavaScript. There are other types of operators as well, such as bitwise operators, conditional (ternary) operator, and more. Understanding how to use these operators effectively is essential for writing efficient and effective JavaScript code.
JavaScript Expressions
In JavaScript, an expression is any valid unit of code that can be evaluated to produce a value. Expressions can be composed of a combination of variables, values, operators, and function calls. Here are some examples of JavaScript expressions:
5 + 7; // returns 12
"Hello, " + "world!"; // returns "Hello, world!"
true && false; // returns false
Math.pow(2, 3); // returns 8
1 + 2 * 3; // returns 7 (follows the order of operations)
As you can see, each of these examples is a valid expression that returns a value. Expressions can be simple or complex, but they must always produce a value. It’s also worth noting that expressions can be assigned to variables, passed as arguments to functions, and used in a variety of other ways in JavaScript code.
Here are some examples of using expressions in JavaScript code:
let x = 5 + 7; // assigns the value 12 to the variable x
console.log("The result is: " + (1 + 2)); // prints "The result is: 3" to the console
function double(num) {
return num * 2;
}
console.log(double(3 + 4)); // calls the double function with the expression 3 + 4 as an argument and prints the result (14) to the console
In summary, expressions are an essential part of JavaScript code and can be used to perform calculations, concatenate strings, evaluate conditions, and much more.
JavaScript Keywords
JavaScript keywords are reserved words that have a specific meaning and purpose in the language. These keywords cannot be used as variable names or function names because they are already used by the language. Here are some of the most commonly used JavaScript keywords:
break
: Used to break out of a loop or switch statement.case
: Used in a switch statement to specify a case.catch
: Used to handle errors in a try-catch statement.const
: Used to declare a constant variable.continue
: Used to continue to the next iteration of a loop.debugger
: Used to set a breakpoint in the code for debugging purposes.default
: Used in a switch statement to specify the default case.delete
: Used to delete a property from an object.do
: Used to start a do-while loop.else
: Used to specify an alternative block of code to execute if a condition is false.export
: Used to export a module or value from a module.extends
: Used to create a subclass of an existing class.finally
: Used in a try-finally statement to specify a block of code that will always be executed.for
: Used to start a for loop.function
: Used to define a function.if
: Used to specify a condition to execute a block of code.import
: Used to import a module or value from a module.in
: Used to check if a property exists in an object.instanceof
: Used to check if an object is an instance of a specific class.let
: Used to declare a block-scoped variable.new
: Used to create a new instance of an object.return
: Used to return a value from a function.super
: Used to call a method or constructor of a parent class.switch
: Used to specify multiple alternative blocks of code to execute.this
: Refers to the current object.throw
: Used to throw an exception.try
: Used to start a try-catch or try-finally statement.typeof
: Used to get the data type of a value.var
: Used to declare a variable.void
: Used to specify that a function does not return a value.while
: Used to start a while loop.with
: Used to specify a default object for properties.
These keywords are an important part of JavaScript syntax and understanding their usage is crucial for writing effective code. It’s important to remember that these keywords cannot be used as variable names or function names, as doing so will result in a syntax error.
JavaScript Comments
In JavaScript, comments are used to provide explanations, descriptions, or notes about the code. Comments are ignored by the JavaScript interpreter and are not executed as part of the program.
There are two types of comments in JavaScript:
- Single-line comments: Single-line comments begin with two forward slashes (
//
). Everything that follows on the same line after the//
is considered a comment and is ignored by the interpreter. Here’s an example of a single-line comment in JavaScript:
// This is a single-line comment
- Multi-line comments: Multi-line comments begin with
/*
and end with*/
. Everything between the/*
and*/
is considered a comment and is ignored by the interpreter. Multi-line comments are useful for providing longer explanations or for temporarily disabling parts of the code. Here’s an example of a multi-line comment in JavaScript:
/*
This is a multi-line comment.
It can span multiple lines.
*/
Comments are important for documenting code and making it easier to understand and maintain. They can be used to explain the purpose of a function, describe a particular piece of code, or provide instructions for other developers working on the project.
It’s important to note that comments should be used judiciously and not be overused to the point of making the code difficult to read. Good code should be self-explanatory and well-organized, with comments used sparingly to provide additional context where necessary.
JavaScript Identifiers / Names
In JavaScript, identifiers are used to name variables, functions, and other constructs in the code. An identifier is a sequence of characters that consists of letters, digits, underscores, and dollar signs. The first character must be a letter, an underscore, or a dollar sign. Identifiers are case sensitive, which means that foo
and Foo
are considered two different identifiers.
Here are some rules and conventions for naming identifiers in JavaScript:
- Use descriptive names: Choose names that are descriptive and meaningful. For example, instead of naming a variable
x
, you could name itnumOfStudents
. - Use camelCase: In JavaScript, it’s common to use camelCase to name variables, functions, and objects. CamelCase is a naming convention where the first letter of the first word is lowercase and the first letter of each subsequent word is capitalized. For example,
firstName
,calculateTotal
, andmyObject
. - Don’t use reserved words: Avoid using reserved words as identifiers, as they have a specific meaning in JavaScript and cannot be used as variable or function names. Examples of reserved words include
if
,else
,while
,for
,function
,var
,const
,let
,true
,false
,null
, andundefined
. - Follow naming conventions: There are some common naming conventions used in JavaScript, such as using all caps for constants (
MAX_VALUE
), prefixing private variables with an underscore (_privateVar
), and using uppercase for constructor functions (Person
). - Be consistent: Use consistent naming conventions throughout your code to make it easier to read and maintain.
Overall, choosing good names for identifiers in JavaScript is important for writing clear, readable code that is easy to understand and maintain.
JavaScript is Case Sensitive
Yes, JavaScript is a case-sensitive language, which means that myVariable
and myvariable
are considered two different variables.
For example, consider the following code:
let myVariable = 10;
let MyVariable = 20;
console.log(myVariable); // output: 10
console.log(MyVariable); // output: 20
In this code, we have declared two variables with similar names but different capitalization. When we log the values of these variables to the console, we get two different outputs.
It’s important to keep in mind that JavaScript is case-sensitive when writing code, and to use consistent capitalization for identifiers throughout your code to avoid errors and confusion.
JavaScript and Camel Case
In JavaScript, it’s common to use camel case when naming variables, functions, and objects.
Camel case is a naming convention where the first letter of the first word is lowercase and the first letter of each subsequent word is capitalized, with no spaces or underscores between the words. For example:
let firstName = "John";
let lastName = "Doe";
let fullName = firstName + " " + lastName;
function calculateTotal(price, quantity) {
return price * quantity;
}
let myObject = {
firstName: "John",
lastName: "Doe",
age: 30
};
In this code, we have used camel case to name the variables firstName
, lastName
, and fullName
, the function calculateTotal
, and the object myObject
.
Using camel case makes it easier to read and understand your code, as it helps to differentiate between words in a variable or function name. It also makes your code more consistent and easier to maintain.
JavaScript Character Set
The character set in JavaScript is Unicode, which is a universal character encoding standard that can represent almost all of the characters from all of the writing systems in the world.
Unicode assigns a unique code point to each character, which is represented by a numerical value. In JavaScript, these numerical values can be represented using escape sequences, such as \uXXXX
, where XXXX
is the Unicode code point in hexadecimal format.
For example, the Unicode code point for the letter “A” is U+0041
, which can be represented in JavaScript using the escape sequence \u0041
. Here’s an example:
let myString = "\u0041BC"; // "ABC"
In this code, we have assigned the string “\u0041BC” to the variable myString
. When we log the value of myString
to the console, we get the output “ABC”, as the escape sequence \u0041
represents the letter “A”.
Unicode in JavaScript allows for the use of characters from a wide range of writing systems, including Latin, Cyrillic, Greek, Chinese, Japanese, and many others. This makes it possible to write JavaScript code in multiple languages and to create web applications that support multiple languages and writing systems.
![]() |
JavaScript Statements
1 week ago -2 likes - 26 views - No comment |
![]() |
JavaScript Tutorial
1 week ago -4 likes - 55 views - No comment |
![]() |
Computer Basics of C Programing
10 months ago -7 likes - 181 views - No comment |
No comments to “JavaScript Syntax” |