জাভাস্ক্রিপ্ট কি এবং কেন শিখবেন ও প্রাথমিক জাভা কোডিং

জাভাস্ক্রিপ্ট  ইন্টারনেটে ব্যবহৃত সবচেয়ে জনপ্রিয় স্ক্রিপ্টিং ল্যাংগুয়েজ। জাভাস্ক্রিপ্ট হল একটি ক্লায়েন্ট সাইড স্ক্রিপ্টিং বা ব্রাউজার স্ক্রিপ্টিং ল্যাংগুয়েজ। জাভাস্ক্রিপ্ট (সংক্ষেপে JS বলা হয়) একটি প্রোটোটাইপ-ভিত্তিক স্ক্রিপ্টিং ল্যাংগুয়েজ যাতে পরিবর্তনশীল, দুর্বল টাইপ এবং প্রথম শ্রেণীর ফাংশন আছে। এটি একটি অবজেক্ট ওরিয়েন্টেড, কার্যকরী প্রোগ্রামিং শৈলী সমর্থনকারী ভাষা। জাভাস্ক্রিপ্ট একটি ওবজেক্ট-ওরিয়েন্টেড, ডায়নামিক প্রোগ্রামিং ভাষা। এতে আছে ডেটা টাইপ, অপারেটর, গুরুত্বপূর্ণ কিছু অবজেক্ট (যেগুলো সব সময় আপনি ব্যবহার করতে পারবেন) আর ফাংশন বা মেথড। জাভা আর সি প্রোগ্রামিং ভাষা থেকে বেশ কিছু সিনট্যাক্স ধার করে নেওয়ায় যারা এসব ভাষায় পারদর্শী তাদের জন্য এটি শেখা তুলনামূলকভাবে সহজ।

১৯৯৫ সালে নেটস্কেপের প্রকৌশলী ব্রেন্ডন আইক জাভাস্ক্রিপ্ট তৈরি করেন, যেটা মুক্তি পায় ১৯৯৬ সালের শুরুর দিকে নেটস্কেপ ২ (ব্রাউজার) এর সাথে। এর নাম দেয়া হয়েছিল LiveScript , কিন্তু মার্কেটিং কৌশলের গ্যাড়াকলে পড়ে দুর্ভাগ্যজনত এর নাম জাভাস্ক্রিপ্ট হয়ে যায়,  সান মাইক্রোসিস্টেম এর জাভা ল্যাংগুয়েজের জনপ্রিয়তাকে পুঁজি করার জন্য। জাভা আর জাভাস্ক্রিপ্টের মাঝে তেমন কোন মিল না থাকা সত্ত্বেও সেই থেকে তাই জাভাস্ক্রিপ্ট নামটা নিয়ে বিভ্রান্তি থেকে গেছে। মাইক্রোসফট এই প্রোগ্রামিং ভাষার সাথে প্রায় মিলে যায় এরকম একটি ল্যাংগুয়েজ JScript নাম দিয়ে প্রায় ৩ মাস পর ইন্টারনেট এক্সপ্লোরারের সাথে বাজারে নিয়ে আসে। এদিকে নেটস্কেপ Ecma International (স্ট্যান্ডার্ড নির্ধারণ করে এরকম একটি ইউরোপীয়ান সংস্থা) এর কাছে ল্যাংগুয়েজটি উপস্থাপন করে - যার ফলাফল ১৯৯৭ সালে ECMAScript এর প্রথম সংস্করণ হিসেবে বাজারে আসে। ১৯৯৯ সালে এই স্ট্যান্ডার্ডটি আরো উন্নত হয় ECMAScript সংস্করণ ৩ হিসেবে - আর সেই থেকে ভাষাটির তেমন কোন বড় পরিবর্তন হয়নি। চতুর্থ সংস্করণটি ভেস্তে যায়, ভাষাটির জটিলতা নিয়ে মতবিরোধের ফলাফল হিসেবে। তবে এই চতুর্থ সংস্করণের অনেক অংশবিশেষ কে ভিত্তি হিসেবে ধরে ২০০৯ সালে নতুন ECMAScript এর পঞ্চম সংস্করণ প্রকাশ করা হয়।

জাভাস্ক্রিপ্ট এ ভেরিয়েবল ডিক্লায়ার করা হয় var কি ওয়ার্ড দিয়ে

var x; // defines the variable x and assigns to it the special value "undefined" (not to be confused with an undefined value)
var y = 2; // defines the variable y and assigns to it the value 2

এই ভাষায় টিউমেন্ট লিখা হয় ২টা স্ল্যাশ এর মাধ্যমে (//)। যেটা উপরে দেখতে পাচ্ছেন।

এর কোন বিল্ড ইন ফাংশান নাই ; রান টাইম ইনভাইরন মেন্ট এটা প্রদান করে থাকে. The ECMAScript এর ৫ম এডিশনে বলা হয়েছে:[৭]

… এখানে কোন প্রভিশনের প্রয়োজন নাই। ডাটা স্ফেকিশনের জন্য.

যদিও অধিকাংশেরকনসল অবজেক্ট থাকে  যা দ্বারা সে এর আউটপুট দেখতে পারে :

console.log("Hello World!");

জাভাস্ক্রিপ্ট এর একটা সিম্পল ফাংশন:

function factorial(n) {if (n=0|n=1) return 1; //fact0=fact1=1 return n * factorial(n - 1)}

factorial(3); // returns 6

একটা এনোনমাস ফাংশন :

function counter() {
var count = 0; return function() {return +count};

var closure = counter();
closure(); // returns 1
closure(); // returns 2
closure(); // returns 3
function sum() {var x = 0; for (var i = 0; i < arguments.length; +i) x += arguments[i]; return x}

sum(1,  2); // returns 3
sum(1,  2,  3); // returns 6
var counter = (function () { var i = 0; // private property return { // public methods get: function () { alert(i); },  set: function (value) { i = value; },  increment: function () { alert(+i); } };
})(); // module

counter.get(); // shows 0
counter.set(6);
counter.increment(); // shows 7
counter.increment(); // shows 8

আরো একটি জাভাস্ক্রিপ্ট এর উদাহরন দেখুন ঃ

এই সাধারন কোড জাভাস্ক্রিপ্ট এর বিভিন্য উদাহরণ প্রকাশ করে। .

/* Finds the lowest common multiple (LCM) of two numbers */
function LCMCalculator(x,  y) { // constructor function var checkInt = function (x) { // inner function if (x % 1 != 0) { throw new TypeError(x + " is not an integer"); // throw an exception } return x; }; this.a = checkInt(x) // semicolons ^  are optional, a newline is enough this.b = checkInt(y);
}
// The prototype of object instances created by a constructor is
// that constructor's "prototype" property.
LCMCalculator.prototype = { // object literal constructor: LCMCalculator,  // when reassigning a prototype, set the constructor property appropriately gcd: function () { // method that calculates the greatest common divisor // Euclidean algorithm: var a = Math.abs(this.a),  b = Math.abs(this.b),  t; if (a < b) { // swap variables t = b; b = a; a = t; } while (b != 0) { t = b; b = a % b; a = t; } // Only need to calculate GCD once, so "redefine" this method. // (Actually not redefinition—it's defined on the instance itself,  // so that this.gcd refers to this "redefinition" instead of LCMCalculator.prototype.gcd. // Note that this leads to a wrong result if the LCMCalculator object members "a" and/or "b" are altered afterwards.) // Also, 'gcd' = "gcd", this['gcd'] = this.gcd this['gcd'] = function () { return a; }; return a; },  // Object property names can be specified by strings delimited by double (") or single (') quotes. lcm : function () { // Variable names don't collide with object properties, e.g., |lcm| is not |this.lcm|. // not using |this.a*this.b| to avoid FP precision issues var lcm = this.a/this.gcd()*this.b; // Only need to calculate lcm once, so "redefine" this method. this.lcm = function () { return lcm; }; return lcm; },  toString: function () { return "LCMCalculator: a = " + this.a + ", b = " + this.b; }
};

// একটা আউটপুট ফাংশন; এই প্রয়োগ শুধুমাত্র ওয়েব ব্রাওজারে কাজ করবে। 
function output(x) { document.body.appendChild(document.createTextNode(x)); document.body.appendChild(document.createElement('br'));
}

// Note: Array's map() and forEach() are defined in JavaScript 1.6.
// They are used here to demonstrate JavaScript's inherent functional nature.
[25,  55],  [21,  56],  [22,  58],  [28,  56].map(function (pair) { // array literal + mapping function return new LCMCalculator(pair[0],  pair[1]);
}).sort(a,  b) => a.lcm() - b.lcm()) // sort with this comparative function; => is a shorthand form of a function, called "arrow function" .forEach(printResult);

function printResult(obj) { output(obj + ", gcd = " + obj.gcd() + ", lcm = " + obj.lcm());
}

নিচের উদাহরনটা ওয়েব ব্রাউজারে প্রদর্শন করবে।

LCMCalculator: a = 28, b = 56, gcd = 28, lcm = 56
LCMCalculator: a = 21, b = 56, gcd = 7, lcm = 168
LCMCalculator: a = 25, b = 55, gcd = 5, lcm = 275
LCMCalculator: a = 22, b = 58, gcd = 2, lcm = 638

Level 0

আমি মো নূর আলম। বিশ্বের সর্ববৃহৎ বিজ্ঞান ও প্রযুক্তির সৌশল নেটওয়ার্ক - টেকটিউনস এ আমি 3 বছর 10 মাস যাবৎ যুক্ত আছি। টেকটিউনস আমি এ পর্যন্ত 9 টি টিউন ও 1 টি টিউমেন্ট করেছি। টেকটিউনসে আমার 0 ফলোয়ার আছে এবং আমি টেকটিউনসে 0 টিউনারকে ফলো করি।


টিউনস


আরও টিউনস


টিউনারের আরও টিউনস


টিউমেন্টস