Basic knowledge to know about variables and java script

         

JavaScript is a high-level, interpreted programming language that is widely used for front-end web development. It is used to add interactivity, animation, and dynamic updates to websites, making the user experience more dynamic and engaging. JavaScript is also used for server-side programming with technologies such as Node.js. JavaScript is used for creating browser-based games, mobile apps, desktop applications, and more. Additionally, JavaScript is an essential component of full-stack web development, allowing developers to create dynamic and interactive web pages on the client-side, as well as integrate with back-end systems.

What are variables in javascript?

In JavaScript, a variable is a container that holds a value, which can be of various data types such as string, number, boolean, etc. Variables are declared using the "var", "let", or "const" keywords, followed by a name for the variable. The value of a variable can be changed during the execution of a program. For example:

let name = "John"; console.log(name); // outputs "John" 

name = "Jane"; console.log(name); // outputs "Jane"

How to declare variables? 

In JavaScript, you can declare variables using the "var", "let", or "const" keywords.

var: The "var" keyword is used to declare variables that are either function scoped or globally scoped, depending on where they are declared. Variables declared with "var" are hoisted to the top of their scope and can be re-declared multiple times.

Example:
var x = 10; var x = 20; console.log(x); // outputs 20

let: The "let" keyword is used to declare variables that are block scoped. Variables declared with "let" cannot be re-declared within the same block scope.
Example:

let y = 10; // let y = 20; // Uncaught SyntaxError: Identifier 'y' has already been declared console.log(y); // outputs 10

const: The "const" keyword is used to declare variables that are read-only. Variables declared with "const" cannot be re-assigned a different value within the same block scope.

Example:javascriptCopy code
const z = 10; // z = 20; // Uncaught TypeError: Assignment to constant variable. console.log(z); // outputs 10

How to assign value to variable after declaration?

You can assign a value to a variable after it has been declared using the assignment operator =.
Example:
let x;
x = 10;
console.log(x); // outputs 10

x = 20;
console.log(x); // outputs 20

Note: When declaring a variable using "const", its value must be assigned during the declaration and cannot be changed later.
Example:

const y = 10;

// y = 20; // Uncaught TypeError: Assignment to constant variable.
console.log(y); // outputs 10

Name different types of data we can store in a variable.

JavaScript, there are several data types that can be stored in a variable:

Number: used to represent numeric values, such as 10, 3.14, etc.
  • String: used to represent text values, such as "Hello, World!", "John", etc. Strings must be enclosed in quotes (either single or double). 
  • Boolean: used to represent logical values of either true or false.
  • Object: used to represent complex data structures, such as arrays, functions, and key-value pairs.
  • Array: used to represent ordered lists of values, such as [1, 2, 3], ["apple", "banana", "cherry"], etc.
  • Function: used to represent blocks of code that can be executed whenever they are called.
  • Undefined: used to represent a variable that has been declared but not assigned a value.
  • Null: used to represent a deliberate non-value.
  • Symbol: used to create unique, immutable values for properties.

Each variable can store only one data type at a time, but the type can change dynamically during the execution of a program. JavaScript is a loosely typed language, meaning that the type of a variable does not need to be explicitly declared, but is automatically determined based on the value assigned to it. 

How to access value of a variable?

In JavaScript, you can access the value of a variable by simply referencing its name in your code. For example, if you have a variable named x with a value of 10, you can access its value by using the variable name x in an expression or by printing it using console.log

let x = 10; 
console.log(x); // Output: 10

What will happen if we try to access the variable that doesn’t exist?

If we try to access a variable that doesn't exist in your code, it will raise a NameError exception. For example:

print(y) # Output: NameError: name 'y' is not defined
This means that the variable y has not been defined or assigned a value in your code. To avoid this error, make sure that you have defined and assigned a value to a variable before trying to access it.

What are the naming convention of variable?

Variable names in most programming languages follow certain naming conventions to make the code readable and avoid errors. Here are some common conventions for naming variables:
  • Names can only contain letters, numbers, and underscores, and they cannot start with a number.
  • Variable names should be descriptive and self-explanatory.
  • Variable names should be written in lowercase, and words should be separated by underscores. This naming convention is known as snake_case.
  • Avoid using single letter names and abbreviations that are not widely recognized, unless they are commonly used in your field (such as i for a loop index).
  • Avoid using reserved words (e.g. if, for, while) as variable names.
  • Some programming languages are case-sensitive, so name and Name are treated as different variables.
Example of good variable names:
first_name
last_name
age
student_count









Comments

Popular Posts