MainMenu

Home Java Overview Maven Tutorials

Sunday, 3 November 2024

Java Script Tutorial 3 Data Types in Java Scripts




Data Types in Java Script:

JavaScript, as a dynamically-typed language, provides several data types that help developers define the nature of variables and manipulate data within their programs. These data types include:

1. Primitive Data Types:


Number: Represents both integer and floating-point numbers.
String: Represents a sequence of characters, enclosed in single (' ') or double (" ") quotes.
Boolean: Represents a logical value of either true or false.
Null: Represents the intentional absence of any object value.
Undefined: Represents a variable that has been declared but not assigned any value.
Symbol: Represents unique and immutable values and is often used as object property keys.

Example :

let a = 10;
let b = 15.50;
console.log(a+b);
let str = "chandan";
let str2 = 'singh';
console.log(str + " " +str2);
let str3 = `str ${a*10}`;
console.log(str3);
let bo = true;
if(bo){
console.log("its true")
}




2. Composite Data Types/Non-primitive data type:


Object: Represents a collection of key-value pairs where keys are strings (or Symbols) and values can be of any data type, including functions, other objects, etc.

Array: A special type of object used to store a collection of elements. Arrays can contain any data type, and elements are accessed by their numeric indices. JavaScript also supports special data structures and object types like Map, Set, Date, RegExp, etc., each with its own unique characteristics and functionalities.The dynamic nature of JavaScript allows variables to adapt to different data types as needed during runtime, which gives developers flexibility but also requires careful consideration to avoid unexpected behaviors due to type coercion or implicit type conversions.

ex : let arrayName = [element1, element2, element3, ...];

Objects in Java Script

In JavaScript, objects are a fundamental data type that allows for the creation of collections of key-value pairs. Objects are used to store data in the form of properties and methods. They are versatile and can represent complex entities by grouping related data and functionalities together.

Objects in JavaScript are defined using curly braces {} and contain zero or more key-value pairs. Keys (also known as property names) are strings or Symbols, and values can be of any data type, including other objects, arrays, functions, etc.

Here's an example of creating an object in JavaScript:



Array in Java Script


In JavaScript, an array is a collection of elements, which can include numbers, strings, objects, other arrays, and more. Arrays are particularly useful for storing lists of items and are zero-indexed, meaning the first element has an index of 0.
Key Features of Arrays in JavaScript
Flexible Data Types: Arrays can hold any data type, including strings, numbers, objects, and even other arrays (making it possible to create multidimensional arrays).
Example :
let mixedArray = [42, "hello", true, { name: "Alice" }, [1, 2, 3]];

Dynamic Length: The length of an array can change dynamically. You can add or remove elements as needed.
Accessing Elements: Elements are accessed by their index, starting from 0
Example :
let fruits = ["apple", "banana", "cherry"];
console.log(fruits[1]); // Output: "banana"

Array Properties and Methods:
length: Returns the number of elements in the array.
Example :
console.log(fruits.length); // Output: 3

push(element): Adds one or more elements to the end of the array.
Example :
fruits.push("orange"); // ["apple", "banana", "cherry", "orange"]

pop(): Removes the last element from the array and returns it
Example :
let lastFruit = fruits.pop(); // "orange"

unshift(element): Adds one or more elements to the beginning of the array
Example :
fruits.unshift("kiwi"); // ["kiwi", "apple", "banana", "cherry"]

shift(): Removes the first element from the array and returns it.
Example :
let firstFruit = fruits.shift(); // "kiwi"

splice(start, deleteCount, ...items): Adds/removes elements from a specific position in the array.
Example :
fruits.splice(1, 1, "grape"); // Replaces "apple" with "grape"

slice(start, end): Returns a shallow copy of a portion of an array
Example :
let newFruits = fruits.slice(1, 3); // ["banana", "cherry"]

indexOf(element): Returns the index of the first occurrence of the specified element, or -1 if it does not exist.
Example :
console.log(fruits.indexOf("cherry")); // Output: 2

Java Script Tutorial 2 Variables in Java Script




Variables in JavaScript

1) Variable names are case sensitive means “way2testing” and “WAY2TESTING” are different.

2) Space is not allowed only letter, digit, underscore and $ is allowed in variable name.

3) Reserved words cannot be used as variable name like “log, break, case, catch etc”.

4) Variable name’s first character must be only a letter, underscore or $.

Note : In general variable name should be in camel case.

Variable name can be used with three keywords

var : Variable can be re-declared and updated. A global Scope variable.
let : Variable can’t be re-declared but can be updated. A block scope variable.
Const: Variable can’t be re-declared or updated. A block space variable.

Note : const variable generally defined in capital letter.





JavaScript Tutorials

Java Script Topics Java Script Topics
1.

Java Script Tutorial 1 Introduction

2.

Java Script Tutorial 2 Variables in JavaScript

3.

Java Script Tutorial 3 Data Types, Object, Array in Java Script

4.

Java Script Tutorial 4

5.

6.

7.

8.

9.

10.

11.

12.

13.

14.

15.

16.

Java Script Tutorial 1 -- Introduction of java script, Syntax of Java Script




JavaScript Introduction 1:

JavaScript is a high-level, versatile programming language primarily used to create interactive effects within web browsers. Developed by Netscape, it was initially named LiveScript before being renamed JavaScript.

Key aspects of JavaScript include:

Client-Side Scripting: JavaScript is primarily used for client-side web development, allowing developers to create dynamic content that interacts with users, modifies the content of web pages, and responds to events triggered by users' actions (like clicks, form submissions, etc.).

Object-Based Language: JavaScript is object-based, meaning it uses objects and their properties to build scripts and functionalities. Objects in JavaScript can be predefined (like Date, Math, etc.) or custom-defined by developers.

Versatility: Originally created for web browsers, JavaScript has expanded its scope and can now be used for server-side development (Node.js), mobile app development (React Native, NativeScript), game development (using frameworks like Phaser, Three.js), and more.

Syntax: The syntax of JavaScript is similar to other programming languages like Java and C, making it relatively easy to learn for those familiar with programming concepts.

Interactivity and Dynamic Content: JavaScript allows for the creation of interactive elements on web pages, such as form validation, animations, dynamic updates without reloading the page (AJAX), and more.

Libraries and Frameworks:There are numerous libraries and frameworks built on top of JavaScript (e.g., jQuery, React.js, Angular.js, Vue.js) that simplify and streamline the development process, providing reusable components and enhancing the functionality of JavaScript.

Cross-Browser Compatibility: JavaScript is supported by all major web browsers like Chrome, Firefox, Safari, and Edge, ensuring cross-browser compatibility for web applications.

JavaScript plays a crucial role in modern web development, allowing developers to create rich, interactive, and user-friendly web experiences. Its versatility and widespread adoption have made it an integral part of web development ecosystems.




JavaScript Syntax :


0. Use the curly braces ({}) to create a block that groups one or more simple statements.

1.Javascript ignore the white spaces

2.In javascript (“;” ) used to end a statement and it is optional

3.Single line comment like “//”

4. Multiple line comment should be like “/* …… */”

For Example :

console.log("chandan");
let a = 10;
let b = 20;
//console.log(a+b);
console.log(a+b+30);
/*console.log("comment me)
console.log("comment me")
console.log("comment me")*/